- Home /
Converting mouse position to worldpoint in 3d
I had this script for moving player in my 2d game:
void Update () {
if (Input.GetMouseButton(1))
{
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
force = Vector2.ClampMagnitude(new Vector2((mousePos.x - transform.position.x), (mousePos.y - transform.position.y)), maxControlRadius);
PlayerRB.velocity = force * speed;
} }
But now i want to upgrade to a 3d project. The problem is that i just dont know how to get the right translation from screen to world point coordinates. I have camera above the player looking down at him. But when i try to do this:
Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane));
it always giving me coordinates (-0.1, 0.97 , 3.0). I mentioned that they are similiar to camera position (0, 10, 3).
The thing is that i want to move player only in the X and Z axis. What am i doing wrong? Pleeeeease help me.
Answer by Cornelis-de-Jager · Aug 22, 2017 at 10:41 PM
Hi Mixi777,
It looks like you are only working with the positioning vectors instead of directional vectors. What you have to do is find the point you click and the point your transform is at. Then minus the player position from the target location to get direction.
That will give you a direction. The trick then is to move your player, but you have to decide if it will be in relation to the world or the player. It seems like you are referring to the world. See code below:
// Cast a ray from screen point
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Save the info
RaycastHit out;
// You successfully hi
if (Physics.Raycast (ray, out hit)) {
// Find the direction to move in
Vector3 dir = hit.point - transform.position;
// Make it so that its only in x and y axis
dir.y = 0; // No vertical movement
// Now move your character in world space
transform.Translate (dir * Time.DeltaTime * speed, Space.World);
// transform.Translate (dir * Time.DeltaTime * speed); // Try this if it doesn't work
}
Of course this is just the base function that doesn't take the players rotation into account. If this is indeed a click to move game, then I highly recommend that you check out NavMeshes. It is very simple to implement and quick to learn.
But if it is simply to move to a location then use the code above. Change the translate to your velocity code.
But Like I said, this doesn't take rotation into account. If you want rotation then use the following code:
// Find the look at location
Vector3 target = Camera.main.ScreenPointToWorld (Input.mousePosition);
// Make it so that player always looks forward (prevents entire body goign horizontal)
target.y = transform.position.y + transform.localScale.y;
// Look at the target location
transform.LookAt (target);
Answer by unidad2pete · Aug 22, 2017 at 10:35 PM
public Camera yourCam;
if(Input.GetMouseButton(0))
{
Ray ray = yourCam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
Debug.DrawLine(transform.position, hit.point);
}
https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
https://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
Answer by Mixi777 · Sep 04, 2017 at 05:43 AM
Thanks all of you guys. You totaly made my day. I was strugling so hard.