[SOLVED] Distance between player and mouse

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?

Hello @yaustar, you should see that in my code. I think a straight line between the player’s world position and the mouse’s raycast hit.

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.

Is this what you are currently doing?

Sorry but I don’t understand your questions. What I do in my code I sent with my question. It is based on this tutorial: https://developer.playcanvas.com/en/tutorials/point-and-click-movement/.

Can you post a link to the project please and the steps needed to reproduce the issue?

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.

https://playcanvas.com/project/678528/overview/movement

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.

Can you show an image of what you are expecting it to look like? I think I have a rough idea but I just want to be sure.

Something like this? https://playcanvas.com/editor/scene/906218

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?

Where from? :eyes:

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 :slight_smile:

https://mandrillapp.com/track/click/4832434/playcanvas.com?p=eyJzIjoiNEVQa1BDRnNlTGYyUkJxdGc5akVQNW1CNWk4IiwidiI6MSwicCI6IntcInVcIjo0ODMyNDM0LFwidlwiOjEsXCJ1cmxcIjpcImh0dHBzOlxcXC9cXFwvcGxheWNhbnZhcy5jb21cXFwvZWRpdG9yXFxcL3NjZW5lXFxcLzkwNjIxOFwiLFwiaWRcIjpcIjJhYjY2YWJhZTg4MzQwN2U5YWViMjMyNDMxNmViOGZkXCIsXCJ1cmxfaWRzXCI6W1wiNDVmMTFiZDgwYWUyMDM5ZjcxYTY0NTczMWFmODA4Y2QzNTU4YmJjZlwiXX0ifQ

Yes that’s good news! Any idea how long that will take?

Don’t hold me to the date but hopefully in the next week or so.

Is that in a new release so I know when it will be available?

I would like to know how I can convert the screen distance to world distance. Or is that very complex?

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?

Will should do an announcement in the forums of a new engine release.