Hello Guest

Author Topic: Joystick Rotation Problem  (Read 3916 times)

JBabz

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 42
  • @joe_babz
    • View Profile
    • Zombit on Facebook
Joystick Rotation Problem
« on: August 25, 2013, 03:43:45 am »
This code should make the player rotate (he's on the XY plane) according to joystick movements.

Just straight up, what's wrong with this code? (not to be cliche or anything)

Code: [Select]
    private void joystickMovementUpdate() {
       direction = new Vector3(Input.GetAxis ("Right Joystick Horizontal"), Input.GetAxis ("Right Joystick Vertical"), 0);
 
       horizontalAxis = Input.GetAxis ("Left Joystick Horizontal");
       verticalAxis = Input.GetAxis ("Left Joystick Vertical");
 
       if (direction.sqrMagnitude > 0.1f) {
         qTo = Quaternion.LookRotation(direction);
         qTo.eulerAngles = new Vector3(0, 180, qTo.eulerAngles.z);
       }
 
       transform.eulerAngles = new Vector3(0, 180, transform.eulerAngles.z);
       transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, ROTATION_SPEED * Time.smoothDeltaTime);
       move ();
    }

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Joystick Rotation Problem
« Reply #1 on: August 25, 2013, 10:26:37 am »
This looks so complicated for what you're trying to do. Get it working with the atan2 code I posted a while ago, and then it should make a bit more sense how you should do it. If you can assign a value, you just rotate towards that Quaternion.Euler( ... ) instead.