How to animate only y axis?

I am creating a simple animation that moves an object up and down. I create a curve and animate the y axis, with x and z set at 0. The problem is that these objects can appear at different points in the game, and when they are animating, they are all being transported to the 0 x coordinate because of the animation. I want them to stay at whatever x position they were instantiated at. How do I solve this problem?

If you need to manually control values of some axes you can create dummy parent object for the animated one and transform it.

Delete the keyframes of the x and z axis, That should fix it

Is this problem solved?

“Delete the keyframes of the x and z axis, That should fix it”

This does not work :confused:

Lock the X and Z. Then the object can only move on the Y axis not anything else!

I imagine the reason this happens is because the position of a Transform is a Vector3 struct property and the entire struct must be set at once. So just like you cannot do transform.position.y = 5 you cannot animate only the transform.position.y. You think after 5 iterations of Unity they would change their code to read the struct, set the one axis, and then write it back.

You should that question.

Open the Animation Window from the Window menu. It will ask for an object so drag the object which you want to animate. Then click on the button after dragging the object and it will ask you where to store your animation clip. then, to animate only the y-axis, firstly in the 0 frame, create a keyframe where you didn’t make any changes to your object and go to some other frame and there increase the scale on the y-axis and then play your game. Any doubts just ask me.

Animation is one of the most annoying things in Unity. It is just poorly implemented from the way you trigger animations to play to the way you can’t set individual points in a vector3 for reusability.


Say you want to make a mechanical robot-building game in VR and the user animates each screw by tapping the head of the screw with a screwdriver. You want to make a prefab of the screw but then the animation fails because it can’t just move up and down. on the prefabs y-axis instead, it also saves the X and Z axes of the vector 3 struct!


Others have said you can delete the keys for x and Z but transform position, rotation, and scale cant’ be partially animated. So if you remove the keyframe for x and/or z that value will be animated to a default value of 0 moving the prefab to the wrong location when you play the game. If you add a rigid body and freeze the positions it will do nothing to the animation as that only affects its physics movement, not the keyframed movement that requires a saved X, Y, and Z position. So @sotirosn is correct in stating why this happens and all the other answerers to this question fail because Unity failed to make this possible with their animation tools.


Maybe one day Unity will fix it or an asset will make it possible but in the meantime, the best way is to code the motion. In the above robot-building VR game example, you could still have an animation for the rotation of the screw but the movement on the prefabs y-axis would be done by triggering a script, and if you’re using a script any way you can trigger the rotation at the same time just as “easily”.


using UnityEngine;

public class MoveObject : MonoBehaviour
{
    // Public variables that can be set in the Inspector
    public float animationTime;
    public Vector3 rotationAxis;
    public Vector3 movementAxis;
    public float movementSpeed = 0.1f;
    public float rotationSpeed = 0.1f;
    public bool moveObject = false;
    public bool rotateObject = false;
    public bool toggleDirection = false;
    public bool playOnce = false;
    public bool test = false;

    // Private variables
    private bool animating = false;
    private Vector3 initialRotation;
    private Vector3 initialPos;
    private float currentAnimationTime;
    private bool direction = true;
    private bool firstPlay = true;
    private bool rotateDirection = true;
    private bool canPlay = true;
    private float delayTime;

    void Update()
    {
        // If we're animating
        if (animating)
        {
            // If we're moving the object
            if (moveObject)
            {
                transform.Translate(movementAxis * movementSpeed * Time.deltaTime * (direction ? 1 : -1));
            }

            // If we're rotating the object
            if (rotateObject)
            {
                transform.Rotate(rotationAxis * rotationSpeed * Time.deltaTime * (rotateDirection ? 1 : -1));
            }

            currentAnimationTime += Time.deltaTime;
            if (currentAnimationTime >= animationTime)
            {
                animating = false;
                delayTime = Time.time + animationTime + 1;
            }           
        }

        if (test == true)
        {
            Play();
            test = !test;
        }
    }

    public void Play()
    {
        if (!canPlay || Time.time < delayTime)
        {
            return;
        }

        animating = true;
        currentAnimationTime = 0f;

        if (toggleDirection)
        {
            if (firstPlay)
            {
                firstPlay = false;
            }
            else
            {
                direction = !direction;
                rotationSpeed = -rotationSpeed;
            }
        }

        if (playOnce)
        {
            canPlay = false;
        }
    }

    public void Stop()
    {
        animating = false;
    }
}

Set the rotation and movement axis with 1s and 0s and the amount of rotation and movement with the speed variables.

@brittany It’s very easy.
you have to add rigidbody to the object and freeze positions of x and z.This is the easiest way
Happy to help you.
Thank you.