Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Rotation with right joystick not accurate

Discussion in 'Scripting' started by AlexandreH5, Aug 12, 2019.

  1. AlexandreH5

    AlexandreH5

    Joined:
    Sep 24, 2013
    Posts:
    37
    Hello everyone,

    I have a problem with the right joystick that allows me to rotate my main character Rigidbody on the Y axis (top down view).

    But when I use the right stick it is like magnetic. And so go 45 degrees then 90, 135 etc...
    I have to go very slowly to reach precise degrees and he has a tendency to snap very fast on the numbers above, like a 8 directions rotation...

    My code:
    Code (CSharp):
    1.             Vector3 PlayerToJoystick = Vector3.right * Input.GetAxis ("HorizontalRotationJoystick") + Vector3.forward * Input.GetAxis ("VerticalRotationJoystick");
    2.             if (PlayerToJoystick.sqrMagnitude > 0.0f) {
    3.                 Quaternion NewRotatation = Quaternion.LookRotation (PlayerToJoystick, Vector3.up);
    4.                 playerRigidbody.MoveRotation (NewRotatation);
    5.             }

    I specify that adjust (Gravity, Dead, Sensivity) does not change anything.

    Sorry for my bad English and thanks in advance.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    6,605
  3. AlexandreH5

    AlexandreH5

    Joined:
    Sep 24, 2013
    Posts:
    37
    Hello Kurt,

    It's actually smoother, but the problem remains the same. It is fixed on 8 directions. every 45 degree even by removing the snap in input setting.

    Here my new code with your instruction ^^

    Code (CSharp):
    1.             input = Vector3.zero;
    2.             input.x = Input.GetAxisRaw("HorizontalRotationJoystick");
    3.             input.z = Input.GetAxisRaw("VerticalRotationJoystick");
    4.             baseRot = playerRigidbody.rotation;
    5.             heading = (Mathf.Atan2(input.x, input.z)) * Mathf.Rad2Deg;
    6.             target = Quaternion.Euler(0, heading , 0);
    7.             if (input != Vector3.zero) {
    8.                 input = input.normalized;
    9.                 playerRigidbody.MoveRotation(Quaternion.Lerp(baseRot, target, Time.deltaTime * 10));
    10.             }
    Do you have an idea?

    Thank ^^
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    6,605
    Yes, don't use the heading you computed this frame. Keep a persistent actual heading and rotate that heading towards the new 'heading' value each frame, and use that persistent heading to assign your facing.
     
  5. AlexandreH5

    AlexandreH5

    Joined:
    Sep 24, 2013
    Posts:
    37
    Re, I tried but I do it wrong, or it does not work. Do you have an example to show me? When you say "towards" it's "equal"? Or lerp the value to the other?

    Sorry that Joystick disturbe me :p
     
unityunity