How to make player object slightly tilt as it moves left and right?

Hi,

I am a 3d designer venturing for the first time into C# and Unity over the past few days. I have been working on a little space shooter project (following some 3D Buzz tuts) but have been adding elements of my own functionality with some relative success. However, a problem I have been having is establishing how to make the spaceship slightly tilt as it moves in the direction it is travelling (ie just left and right).

The way the game is created I know the ship needs to tilt on the Y Axis (left for positive, right for negative) and will need to hold some relationship to the ships translation. The script for the translation is as follows:

// Amount to move

float amtToMove = Input.GetAxis(ā€œHorizontalā€) * PlayerSpeed * Time.deltaTime;

//Move the player

transform.Translate(Vector3.right * amtToMove);

So how can I add this slight tilt in rotation to my shipā€™s movement? Any help would be much appreciated!

Many Thanks!

Colin.

first tilt the ship to how you would want it to look while moving. then record the rotation (on paper)
the when you move left or right transform.rotate(x,x,x) depend on which axis you are rotating multiply it by Time.DeltaTime

Rotating the ship like this will only cause more problems.

Your best bet is to use the Z axis in the localEulerAngles.

When the player moves left, Lerp the Z axis to about 15 degrees, right, 15 the other way. If he lets go, lerp it back to zero.

Logic is like this:

float z = Input.GetAxis("Horizontal") * 15.0f; // might be negative, just test it
Vector3 euler = transform.localEulerAngles;
euler.z = Mathf.Lerp(euler.z, z, 2.0f * Time.deltaTime);
transform.localEulerAngles = euler;
3 Likes

THANK YOU THANK YOU THANK YOU!!! this completely worksā€¦ i used it for a helicopter. i know its a while later but oh well.

PAULLUS

1 Like

actually i came up with a sort of easier wayā€¦ use animations. it works perfectly for a helicopter. but yours is of course probably a lot better because its in codeā€¦ but oh well.

PAULLUS

I didnā€™t quite get it. So say I have this helicopter object. I want it to tilt a little when move right. What else to I write to get it to tilt?

helicopter.transform.Translate( speed * Time.deltaTime * 20,0,0 );

This one helped me a lot.

Hi! I know this is really old but Iā€™m trying to figure out why itā€™s not working. I implemented this code exactly, but this is what it looks like: https://imgur.com/a/k1VyyET

I tested this in a new Unity project to make sure it was a problem with the code and not with other scripts, and the same behavior occurs.

I had a similar problem but fixed it by removing the ā€˜* Time.deltaTimeā€™.

I really needed this simple code, but Iā€™m having a problemā€¦ I edited it a little bit to work with my code. It works but only if my player doesnā€™t face the camera, otherwise it would tilt the wrong way. Hereā€™s my code:

public class ThirdPersonMovement : MonoBehaviour
{
    public Transform cam;
    private readonly float turnSmoothTime = 0.2f;
    float turnSmoothVelocity;
    private Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector3 direction = new(horizontal, 0f, vertical);
        float magnitude = Mathf.Clamp01(direction.magnitude);
        direction.Normalize();

        float z = Input.GetAxis("Horizontal") * -10.0f;
        Vector3 euler = transform.localEulerAngles;
        euler.z = Mathf.Lerp(euler.z, z, 2.0f);
        transform.localEulerAngles = euler;

        if (direction.magnitude >= 0.2f)
        {
            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
            transform.rotation = Quaternion.Euler(0f, angle, euler.z);
        }

        animator.SetFloat("speed", magnitude, 0.3f, Time.deltaTime);
    }
}

Itā€™s worth noting that I use root motion with IK feet, and it looks fantastic apart from this little problem :frowning:

Itā€™s also worth noting you are necro-posting to a 2012 thread.

Please donā€™t necro-post. If you have a new question, make a new post. Itā€™s FREE!!

Honestly it just sounds like you wrote a bugā€¦ and that meansā€¦ time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Donā€™t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.