- Home /
Changing only one axis(Transform.Position) in Animation Event
I'm first to unity engine and want to make a simple platformer game. Player's original X position is -6, and automatically moves forward changing position.x. I'd like to fix player's y position for between two keyframe when I play an animation, slowly increasing y position before first keyframe and decreasing to original y position after second keyframe. I tried this by adding four keyframes, setting first and last keyframes' position.y to original y value and second and third keyframes' position.y to increased y value. But when I only edit poision.y value, position.x value is automatically fixed to -6(original x value) and player does not move at all. Deleting all position.x keyframe value also yields same result, fixing player's x position. This problem occurs even when I don't play the animation, meaning player's position is fixed from the very beginning of the game. So here is the question : How can I edit only player's y value, not interfering x value?
Answer by Bunny83 · Mar 15, 2018 at 03:03 AM
You can't the localPosition is one property of type Vector3. Animation happens generally in local space. You generally want to avoid animating objects without a root object. You usually animate the child objects which allows you to place and rotate the parent as you like
That means I can change localPosition if I put player object in a parent object? I tried to edit yPos in script but it moved awkwardly so I want to operate this in Animation Event. Can you guide me more?
You don't just put the player object in a parent, but the parent object is your new player object. It's the one that should have the Animation or Animator component and all your player related scripts. Your animation would only animate the visual object which is a child of your actual parent object. Animations in generally animate a character or object in place.
I'm not sure what you mean by "Animation Event". An AnimationEvent is just a single point on the timeline of an animation that fires off an event (calls a method) once the animation passes this point. AnimationEvents have nothing to do with the actual animation (the movement / rotation of objects) but just give you an event at a certain time during the animation.
It's not really clear what this animation should represent or how your final movement should look like. If it should be something like a "jump" animation you usually don't "animate" it. A jump depends on the point where the player jumps off which isn't always the same height. Also a jump could be interrupted due to hitting an obstacle.