It is difficult to explain, but the problem I have is related to the distance between the player and the mouse. When the player is walking, the distance is well determined. However, if the player starts climbing, things go wrong. To solve this I want to rotate the plane I use to determine the position of the mouse 90 degrees. However, the position of the mouse continues to be determined at 0 degrees. How do I fix this? I use the code below:
this.cameraEntity.camera.screenToWorld(event.x, event.y, this.cameraEntity.camera.farClip, this.ray.direction);
this.ray.origin.copy(this.cameraEntity.getPosition());
this.ray.direction.sub(this.ray.origin).normalize();
playerScript.inputPlane = new pc.BoundingBox(inputPlane.getPosition().clone(), inputPlane.getLocalScale().clone());
var result = playerScript.inputPlane.intersectsRay(this.ray, this.hitPosition);
if (result) {
inputPoint.setPosition(this.hitPosition);
}
console.log(player.getPosition().distance(inputPoint.getPosition()));
How are you defining the distance? A straight line between the player’s world position and wherever the mouse’s raycast has hit? Or just along the XZ plane of the world?
What happened now is that when I hold the mouse slightly above the player, the mouse point is moved too far away in space. So to avoid this, I thought it might be a solution if I tilted the plane so that the hit point cannot disappear into space.
That should be straight forward as it’s just the distance between 2 world points?
What happens in the game when the user clicks into empty space?
I assume that everything has a collision mesh so it’s physics raycast into the physics world. If it hits an entity that is part of the level, get the intersection point and calculate the distance between the intersection point and the player.
I have made an example project in which the problem is clearly visible. I want both the moveLine and the climbLine to reach approximately the mouse point. Only I don’t know how I can do this for the climbLine.
PS. I have also a problem if i change to x-axis to 90 degrees of an entity (or -90 degrees, but the front to camera) , the color of the entity is not visible anymore. You can check this also with the inputPlane enitity in this example project.
Yes, but unfortunately I cannot use collision because this will cause problems for all characters in the game. These all have sensors (raycastFirst) for movement. It is very unfortunate that you cannot exclude colliders from this.
The best solution would be if I could also use Bounding Box at 90 degrees. But that is also not possible?
Since my player is always in the center of the screen, I might be able to calculate the distance from the center of the screen to the mouse. @Leonidas has already helped me with this, but this distance still has to be converted to the distance in the world…
PS. I sometimes see a referral from Mandrillapp. Is that okay?
There’s a new API for raycastAll coming soon that will help with this. You can tag the floor/walls with ‘level’ and then use raycastAll to get all the colliders that it hits in a list. As it’s tagged, you can search the list for entities with that tag
It depends on what you need to be calculated since the distance hugely depends on the Z values in the world due to the projection from screen to world space.
ScreenToWorld function from the camera basically does this already. What are you trying to achieve in the screen distance to world distance conversion?