Hello Guest

Author Topic: Character Rotate to Face Mouse  (Read 4340 times)

JBabz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 42
  • @joe_babz
    • View Profile
    • Zombit on Facebook
Character Rotate to Face Mouse
« 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Character Rotate to Face Mouse
« Reply #1 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.