- Home /
2D look at mouse position (z rotation) [c#]
Hello!
I have this zombie, and I want him to look at mouse position http://i.imgur.com/BNAv5NU.png I know that this question is very trivial, but I tried many ways and I still don't know how to solve this problem. Lookat()+Input.mousePosition does not work (it rotates all axis, but not z)
Could somebody help me?
Answer by $$anonymous$$ · Sep 27, 2014 at 06:10 PM
Try using something like this: SOLUTION
float AngleRad = $$anonymous$$athf.Atan2(Input.mousePosition.y - transform.position.y, Input.mousePosition.x - transform.position.x);
float AngleDeg = (180 / $$anonymous$$athf.PI) * AngleRad;
this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
It's supposed to work, but character acts strange, it rotates from time to time, sometimes wrong way, hmm I dunno, any ideas?
damn, totally forgot that pixel position is not world position, thanks!
Vector3 mouseScreenPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 lookAt = mouseScreenPosition;
float AngleRad = $$anonymous$$athf.Atan2(lookAt.y - this.transform.position.y, lookAt.x - this.transform.position.x);
float AngleDeg = (180 / $$anonymous$$athf.PI) * AngleRad;
this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
This works perfect for me, the first one doesn't point to mouse at all. Thanks!!
Answer by Octet_ · Nov 12, 2015 at 01:12 AM
You can simply set the of the basis vectors of the transform directly.
For example if you want the up (green in the scene view) vector to point to the mouse you can write the following code:
// convert mouse position into world coordinates
Vector2 mouseScreenPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// get direction you want to point at
Vector2 direction = (mouseWorldPosition - (Vector2) transform.position).normalized;
// set vector of transform directly
transform.up = direction;
You uses the variable mouseWorldPosition in direction but you make a Vector2 called mouseScreenPosition. Should the name of the variable you make be changed to mouseWorldPosition?
Yes, it needs to be changed to mouseScreenPosition. Update code :
// convert mouse position into world coordinates Vector2 mouseScreenPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// get direction you want to point at Vector2 direction = (mouseScreenPosition - (Vector2) transform.position).normalized;
// set vector of transform directly transform.up = direction;
I was in a tunnel. Your answer was the light that flashed my eyes and let me see the world space. Thnks.
Answer by IsaiahKelly · Mar 17, 2015 at 05:55 PM
Here's a slightly refined and properly formatted solution for others:
Camera cam;
Transform my;
Rigidbody2D body;
void Awake ()
{
cam = Camera.main;
my = GetComponent <Transform> ();
body = GetComponent <Rigidbody2D> ();
}
void Update ()
{
// Distance from camera to object. We need this to get the proper calculation.
float camDis = cam.transform.position.y - my.position.y;
// Get the mouse position in world space. Using camDis for the Z axis.
Vector3 mouse = cam.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, camDis));
float AngleRad = Mathf.Atan2 (mouse.y - my.position.y, mouse.x - my.position.x);
float angle = (180 / Mathf.PI) * AngleRad;
body.rotation = angle;
}
In this example I also rotated the Rigidbody2D instead of the transform. Since it's already a float. You could also do this in Fixed Update.
Answer by Khizbu · Sep 27, 2014 at 08:14 PM
There is full script Vector3 pozycja = Camera.main.ScreenToWorldPoint (Input.mousePosition); float AngleRad = Mathf.Atan2(pozycja.y - transform.position.y, pozycja.x - transform.position.x); float AngleDeg = (180 / Mathf.PI) * AngleRad; this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);
Answer by Bioder · Apr 24, 2015 at 03:51 AM
I found a solution like this:
float camDis = cam.transform.position.y - my.position.y;
Vector3 mouse = cam.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, camDis));
float AngleRad = Mathf.Atan2 (mouse.y - my.position.y, mouse.x - my.position.x);
float angle = (180 / Mathf.PI) * AngleRad;
body.rotation = angle;
But when i'm moving my character it begins to stutter... like from 60 fps to 1-2, but only the flashlight.