2D look at mouse position (z rotation) [c#]

Hello!

I have this zombie, and I want him to look at mouse position

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?

8 Answers

8

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;

nice one, worked perfectly for me !

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?

Thanks It works!!Like Charm

I was in a tunnel. Your answer was the light that flashed my eyes and let me see the world space. Thnks.

Try using something like this: SOLUTION

float AngleRad = Mathf.Atan2(Input.mousePosition.y - transform.position.y, Input.mousePosition.x - transform.position.x); float AngleDeg = (180 / Mathf.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 = Mathf.Atan2(lookAt.y - this.transform.position.y, lookAt.x - this.transform.position.x); float AngleDeg = (180 / Mathf.PI) * AngleRad; this.transform.rotation = Quaternion.Euler(0, 0, AngleDeg);

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.

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);

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.

That should be mouseWorldPosition in line one (obviously), but this is a nice and simple solution!

here is mine:
//finds the gameObject’s position to point torwards(the value and name can be whatever you want to point torwards. Also, if you’re using a gameObject2D/sprite then make sure the gameObject is facing right).
Vector3 cursorGameObject = GameObject.FindWithTag(“CursorGameObject”).transform.position;

    //creates a new float named 'angle', and sets it to: 'Mathf.Atan2()', gets the angle of the given perimeters.
    float angle = Mathf.Atan2(cursorGameObject.y - transform.position.y, cursorGameObject.x - transform.position.x);

    //creates a new float named 'angleCalculated', and sets it to: '(180 / Mathf.PI) * angle', 'Mathf.PI' is math pi: 3.14...
    float angleCalculated = (180 / Mathf.PI) * angle;

    transform.rotation = Quaternion.Euler(0, 0, angleCalculated);,//finds the gameObject's position to point torwards(the value and name can be whatever you want to point torwards. Also, if you're using a gameObject2D/sprite then make sure the gameObject is facing right).
    Vector3 cursorGameObject = GameObject.FindWithTag("CursorGameObject").transform.position;

    //creates a new float named 'angle', and sets it to: 'Mathf.Atan2()', gets the angle of the given perimeters.
    float angle = Mathf.Atan2(cursorGameObject.y - transform.position.y, cursorGameObject.x - transform.position.x);

    //creates a new float named 'angleCalculated', and sets it to: '(180 / Mathf.PI) * angle', 'Mathf.PI' is math pi: 3.14...
    float angleCalculated = (180 / Mathf.PI) * angle;

    transform.rotation = Quaternion.Euler(0, 0, angleCalculated);

This is the most refined version and cleaned of the code

Camera cam;
Transform my;
Rigidbody2D body;

void Start()
{
    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;
}