X, Y Rotation dont go smooth

Hi there,

I’m struggiling quite long with the issue while I’m giving input (new Input system) my rotation x,y goes strciclty from 0 to (updated_value). However in the course video (and other students :P) the rotation goes really smooth. I can’t get why my code don’t rotate like that.

void ProcessRotation()

{  

    float pitch = yThrow * controllPitchFactor;

    float yaw = transform.localPosition.x * controllYawFactor;

    float roll = xThrow * controllRollFactor;

    transform.localRotation = Quaternion.Euler(pitch, yaw, roll);

}

void ProcessTranslation()

{

    xThrow = movement.ReadValue<Vector2>().x;

    yThrow = movement.ReadValue<Vector2>().y;

    float xOffset = xThrow * Time.deltaTime * PlayerSteerSpeed;

    float yOffset = yThrow * Time.deltaTime * PlayerSteerSpeed;

    float rawXPos = transform.localPosition.x + xOffset;

    float rawYPos = transform.localPosition.y + yOffset;

    float clampedXpos = Mathf.Clamp(rawXPos, -xRange, xRange);

    float clampedYpos = Mathf.Clamp(rawYPos, -yRange, yRange);

Hi zeer,

Welcome to our community! :slight_smile:

In the ProcessTranslation method, we use Time.deltaTime for making the movement framerate-independent and smooth. Take a look at your ProcessRotation method. Have you already tried to multiply by Time.deltaTime there?


See also:

Movement is ok Rotation is not smooth. Adding Time.Deltatime anythere in ProcessRotation doesnt give respected results. And the code should be exacly as in tutorial.

It should be or it is? The difference is important. Always verify things because there is so much going on during runtime that even little things could cause big problems.

Did you animate your ship (Player Rig)? If so, make sure that the child (Player Ship) is not animated. Otherwise, it might be that the animation messes around with your values.

If the issue persists, log xThrow and yThrow into your console to see if you get unexpected values when the rotation does not feel smooth.

If the issue persists, log xThrow and yThrow into your console to see if you get unexpected values when the rotation does not feel smooth.

It’s ok. The rotatniont don’t “feel smooth” its jus not smooth :slight_smile: The value from my pad goes form 0 to 1 uning New Input System. And the rotation also goes Input Value * Rotation goes striclty to dedicated rotation number. My rotation of the shi is
0 >> 30
I trying to figure out how to do it
0,1,2,3,4,5, >> 30 in DeltaTime

Could you please share the relevant code with Time.deltaTime, so I can see how you implemented that property?

It’s all about this case. This is a code from tutorial commit. Where I shoud add Time.Deltatime.

Multiply pitch, yaw and roll by Time.deltaTime.

Hey Zeer :slight_smile: The same thing happened to me, so I took a few days out of the course to figure out a workaround. As far as I can see, you’ve already identified the problem. If your xThrow and yThrow are either 0 or 1, then multiplying them with your controllRollFactor and controllPitchFactor (respectively) will act as a toggle. For instance, if your controllRollFactor is 30, then your ship will either be rotated -30, 0, or 30, with no smoothing. I’m guessing that Rick was demonstrating with a thumbstick and getting smoother input values in between 0 and 1, but since I’m working off a keyboard, I had that “toggle” effect. Now… if you’re using a controller and only getting 0/1 as our input with the Input System, then… I can’t help grin I’m about 3 weeks into this course, and that’s also how long I’ve been using Unity (and almost as long as I’ve been familiar with C#), so my workaround isn’t at all the intent of the assignment, and wouldn’t address any deeper issues, but it has smoothed the roll and pitch of my ship, so I’ll paste it here.

public class PlayerControls : MonoBehaviour
{
    [SerializeField] InputAction movement;
    [SerializeField] float moveFactor = 20f;
    [SerializeField] float xRange = 18f;
    [SerializeField] float yRange = 10.5f;
    [SerializeField] float yRangeOffset = 5.5f;

    [SerializeField] float positionPitchFactor = -1f;
    [SerializeField] float controlPitchFactor = -5f;

    [SerializeField] float positionYawFactor = 1.5f;
    [SerializeField] float controlRollFactor = -10f;

    [SerializeField] float rotationLerpIncrement = 3f;          // Lerp variable
    [SerializeField] float rollRotation = 5f;                   // Lerp variable
    [SerializeField] float pitchRotation = 3f;                  // Lerp variable

    float xThrow, yThrow, xLerpThrow, yLerpThrow;               // The last two variables are used in the smooth-rotation code
    float tForRollLerp = 0.5f;                                  // Lerp variable
    float tForPitchLerp = 0.5f;                                 // Lerp variable


    void Update()                                                 // ---UPDATE---
    {
        ProcessTranslation();
        ProcessRotation();
    }

    void ProcessTranslation()                                     // Controls X and Y movement
    {
        xThrow = movement.ReadValue<Vector2>().x;                                   // float xThrow = Input.GetAxis("Horizontal");     // Used for the old input Manager vs...
        yThrow = movement.ReadValue<Vector2>().y;                                   // float yThrow = Input.GetAxis("Vertical");       // ...what this project uses (Input System)

        float xOffset = xThrow * Time.deltaTime * moveFactor;
        float yOffset = yThrow * Time.deltaTime * moveFactor;

        float rawXPos = transform.localPosition.x + xOffset;
        float rawYPos = transform.localPosition.y + yOffset;

        float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
        float clampedYPos = Mathf.Clamp(rawYPos, -yRange + yRangeOffset, yRange + yRangeOffset);

        transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
    }

    void ProcessRotation()                                      // Processes Pitch/Yaw/Roll of the ship
    {
        xLerpThrow = Mathf.Lerp(-rollRotation, rollRotation, tForRollLerp);
        yLerpThrow = Mathf.Lerp(-pitchRotation, pitchRotation, tForPitchLerp);

        float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
        float pitchDueToControlThrow = yLerpThrow * controlPitchFactor;

        float pitch = pitchDueToPosition + pitchDueToControlThrow;
        float yaw = transform.localPosition.x * positionYawFactor;
        float roll = xLerpThrow * controlRollFactor;

        transform.localRotation = Quaternion.Euler(pitch, yaw, roll);



        // Lerp smooth movement code begins here
        if (xThrow > 0)
            tForRollLerp = Mathf.Max(0, tForRollLerp - rotationLerpIncrement * Time.deltaTime);
        else if (xThrow < 0)
            tForRollLerp = Mathf.Min(1, tForRollLerp + rotationLerpIncrement * Time.deltaTime);
        else
            if (tForRollLerp > 0.5)
                tForRollLerp = Mathf.Max(0.5f, tForRollLerp - rotationLerpIncrement * Time.deltaTime);
            else if (tForRollLerp < 0.5)
                tForRollLerp = Mathf.Min(0.5f, tForRollLerp + rotationLerpIncrement * Time.deltaTime);
        
        if (yThrow > 0)
            tForPitchLerp = Mathf.Max(0, tForPitchLerp - rotationLerpIncrement * Time.deltaTime);
        else if (yThrow < 0)
            tForPitchLerp = Mathf.Min(1, tForPitchLerp + rotationLerpIncrement * Time.deltaTime);
        else
            if (tForPitchLerp > 0.5)
                tForPitchLerp = Mathf.Max(0.5f, tForPitchLerp - rotationLerpIncrement * Time.deltaTime);
            else if (tForPitchLerp < 0.5)
                tForPitchLerp = Mathf.Min(0.5f, tForPitchLerp + rotationLerpIncrement * Time.deltaTime);
    }
}

This is my first attempt at posting code (and my first forum post at that :slight_smile: ) so I hope it’s not too long. I basically used xThrow and yThrow only as toggles in the ProcessRotation function, then used Mathf.Lerp to increment from (psudo-code) -pitch/roll to +pitch/roll with the t increment of Mathf.Lerp being at “rest” at 0.5. I created a small sandbox scene in my ship world to test this out, rotating a cube in one direction to a maximum and then having it rotate back to rest when the key was no longer pressed, and then plugged the code into the main script for my ship.
Now, the obvious problem with all this will be that if someone were to plug a controller in to fly my ship, there’s no code to differentiate between keyboard and thumbstick input. So moving the thumbstick only a little would have the correct effect on movement (since xThrow/yThrow will be a number between 0 and 1) but the ship, no matter how slow it’s moving, will rotate to maximum in either direction. …but this is a shortcoming for another day when I’m working on something serious. At the present, it was a good exercise in learning and using Mathf.Lerp (though it still baffles me a bit) and is outside the scope of the assignment.
Anyway, hope that helps (rather than just confusing things). If it doesn’t help… then… erm… back to you, Nina :smiley:

2 Likes

OMG THANX! This worked perfectley for my case :smiley: I tried to make something with Lerp, Math.Lerp more simple but with no effect :stuck_out_tongue: You’ve helped me a lot cause I really struggeled with this quite a long time :smiley: Also your resolution makes this rotation sweet tweakable. Thx very much again!

Nina thaks also, I’ve tried before as you said multiplying by time.deltatime but with no expected effect

1 Like

Woohoo! Glad it helped :smiley: There’s probably a better/easier/cleaner/quicker solution, so I’m keeping my eyes open for that, but for a practice app it’ll suffice for now :slight_smile: In the meantime, it demonstrates a potential “problem” that would have to be kept in mind if you’re using multiple input divices and expecting a thumbstick to behave like WASD. Cross-platform fun for everyone! :smiley: Anyway, good luck with the rest of the course!

Ooh, also, I came across this article on Lerp, and it’s helped clear up a lot of my confusion, so I figured it wouldn’t be tooooo off topic to share it here (since you’d mentioned you tried using Lerp as well) I can’t guarantee everything there is correct or best-practice, but as I say, it helped, so enjoy :slight_smile:

Hmmn… I just read the terms of use, and it looks like I can post links to outside articles (if I’ve overlooked something, please let me know Mods)

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms