- Home /
Question by
AdhamT · Sep 28, 2015 at 06:39 AM ·
c#raycastprogrammingrigidbody2dlookat
How to make a sprite look at mouse on a XY plane ?
Hi !
I am working on a prototype for a top down space shooter on a normal 2D XY plane not a XZ one and I want to make the player look at the mouse.
I tried the raycast solution mentioned in the Survival Shooter project but it didn't work because the Rigidbody2D rotation takes a float not a quaternion.
I tried LookAt and it didn't work at all.
What can I do ?
Thanks in Advance
Comment
Answer by Cherno · Sep 28, 2015 at 10:56 AM
Vector3 lookAtTargetPos;
RigidBody2D rigidBody2D;
void Update() {
lookAtTargetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Quaternion rot = Quaternion.LookRotation(transform.position - lookAtTargetPos, Vector3.forward);
transform.rotation = rot;
transform.eulerAngles = new Vector3 (0, 0, transform.eulerAngles.z);
rigidBody2D.angularVelocity = 0;
}