Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    61
    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:
    38,517
  3. AlexandreH5

    AlexandreH5

    Joined:
    Sep 24, 2013
    Posts:
    61
    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:
    38,517
    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:
    61
    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