2D Toolkit Forum

2D Toolkit => Support => Topic started by: JBabz on August 24, 2013, 03:17:08 am

Title: Character Rotate to Face Mouse
Post by: JBabz on August 24, 2013, 03:17:08 am
I used to have my game on the XZ plane, now I'm changing it to the XY plane.
Can anybody explain why this code doesn't work on the XY plane?

Code: [Select]
private void keyboardMovementUpdate()  {
horizontalAxis = Input.GetAxis ("Keyboard Horizontal");
verticalAxis = Input.GetAxis ("Keyboard Vertical");

Vector3 targetPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
transform.LookAt (new Vector3(targetPoint.x, targetPoint.y, transform.position.z));
// transform.eulerAngles = new Vector3(0, 180, transform.eulerAngles.z); This line "locks" the character in his X and Y rotation.

move ();
}

Uncommenting that line makes the character jump out of rotation, commenting it makes him not move... hmm.
Title: Re: Character Rotate to Face Mouse
Post by: unikronsoftware on August 24, 2013, 12:02:51 pm
Thats because LookAt points the transform
"Rotates the transform so the forward vector points at /target/'s current position."

The forward vector is z, so its not going to work. If its on the XZ plane why would you NOT want to lock the x & y rotation?

And your move function is probably moving using transform.forward, which is the blue axis (z). If you're on the XY plane you probably want to move on either the X or Y.