0
\$\begingroup\$

I wanted to create a very simply turret. This turret should aim at the point on the screen where the mouse pointer is currently located.

I have added a cube to serve as a very simple turret in my scene. To visualize its aiming direction, I'm drawing a ray from it.

enter image description here

To rotate the "turret" to the mouse pointer position, I have created a script and added it to the "turret" gameobject:

void Update()
{
    var mousePos = Input.mousePosition;
    mousePos += Camera.main.transform.forward * 10f; // Make sure to add some "depth" to the screen point 
    var aim = Camera.main.ScreenToWorldPoint(mousePos);

    transform.LookAt(aim);

    RaycastHit hit;

    int layerMask = 1 << 8;
    layerMask = ~layerMask;

    Color color;
    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity, layerMask))
    {
        color = Color.green;

    }
    else
    {
        color = Color.red;
    }

    Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 10000, color);
}

However, there must be something wrong in my code.

In this screenshot, I have added a red arrow that points to the position of the mouse pointer:

enter image description here

The mouse pointer is over that orange tool. However, the turret is rotated towards the floor while it should be rotated upwards to the orange tool.

I do believe that my turret is well positioned (the blue arrow points forward): enter image description here

How do I change my code so that it aims correctly, and what is my mistake?

I guess the problem is in these 2 lines, but I don't see what's wrong about it:

    mousePos += Camera.main.transform.forward * 10f; // Make sure to add some "depth" to the screen point 
    var aim = Camera.main.ScreenToWorldPoint(mousePos);

Thank you.

Edit: I finally understand what Kevin wanted to tell me. I made a thinking mistake, and really need the Raycast to determine where my Worldspace point actually is.

Here is why:

enter image description here

Even though the Y screen coordinate of the mouse position is just a few pixels different in each game window on the right side, the worldspace point is totally different (as can be seen in the scene view window on the left side).

I thought in 2D space, while I should have thought in 3D space.

\$\endgroup\$
0
\$\begingroup\$

Your problem is indeed with the first few lines:

var mousePos = Input.mousePosition;
mousePos += Camera.main.transform.forward * 10f; // Make sure to add some "depth" to the screen point 
var aim = Camera.main.ScreenToWorldPoint(mousePos);

Here you are adding the Camera's forward vector, which is in 3D, to a mouse's 2D coordinates. This is not going to add "depth" to the screen point, it's going to send it off in a seemingly random direction.

Do this:

var mousePos = Input.mousePosition;
mousePos.z = 10; // Make sure to add some "depth" to the screen point 
var aim = Camera.main.ScreenToWorldPoint(mousePos);

Setting the z to a positive value like 10 is all you need to do; this moves the position we got from the cursor "forward" regardless of the orientation of the camera. When you convert to world space, the world space point will be in front of the camera where you expect it to be.

Alternatively, you might need to do a raycast to determine distance if it matters how far away the object under the cursor is:

Ray ray = Camera.main.ScreenPointToRay(mousePos);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
    Vector3 target = hit.point;
}
\$\endgroup\$
1
  • \$\begingroup\$ It would be really nice if you could just add a space or so to your answer, then I can upvote it again. It was really stupid: First I voted it up, then I thought that your answer didn't solve my problem, so I wanted to undo my upvote, and I clicked the down-arrow. However, this didn't result in my upvote being removed, but instead it would even downvote your answer. This was absolutey not what I wanted to do. Then I wanted to click the up arrow again, but stackexchange told me that this is only possible if you edit your question. \$\endgroup\$ – tmighty Jul 19 '20 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.