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)
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?
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?
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.
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;
}
nice one, worked perfectly for me !
– MauroRezendeYou 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?
– OuuGiiiThanks It works!!Like Charm
– OfficeThrashingI was in a tunnel. Your answer was the light that flashed my eyes and let me see the world space. Thnks.
– Raikir-i-sh