Point towards mouse position?

So I’m trying to make a overhead shooter and want it to point towards mouse cursor. So I looked up a vid on that and this is the only code I can find:

   void PointToMouse()
    {

        Vector3 mouse = Input.mousePosition;
        Vector3 ScreenPos = Camera.main.ScreenToWorldPoint(mouse);

        Vector2 offset = new Vector2(mouse.x - ScreenPos.x, mouse.y - ScreenPos.y);

        float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0, 0, angle);
    }

And its seams to be all wonky and doesnt point towards the mouse position at all. Does anyone have any ideas?

Typically the way to do this is to project a ray down from the camera and hit the ground. That’s what the line Camera.main.ScreenToWorldPoint(mouse); is doing.

The position you hit is the positon your character aims towards. You could either user transform.LookAt() to have your player’s transform always looking where the mouse lands on the ground, or you could work out the direction from your character’s position to where the mouse is, calulate the angle and then rotate your character by said angle.

Your code seems to be doing that but I’m not sure what the purpose of the offset is.

Have a watch of this vid as he goes over doing this, including movment for the character.

Oh my gosh thanks for linking brackeys vid. Life saver. Using rb.rotation and rb.position and subtracting 90 from my angle var fixed it.

1 Like

Hey guys, if your still having the problem and seeing that the player is pointing towards your main camera, then go to your main camera and see if it’s on Orthographic and not perspective. The reason is that Orthographic is for 2d projects and perspective is for 3d games. I worked for me so well.

would this work in a 3d project?

Please let us know! But in the meantime…

Please don’t necro-post. If you have a new question, make a new post. It’s FREE!!

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • Do NOT attach entire scripts to your post.
  • ONLY post the relevant code, and then refer to it in your discussion.