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

Moving object in animation - position update

Discussion in '2D' started by OgityBogityBoo, Oct 2, 2018.

  1. OgityBogityBoo

    OgityBogityBoo

    Joined:
    Dec 9, 2015
    Posts:
    15
    For our particular setup we found that animating an object's transform is the best way to move our player in a turn-based game, syncing with the sprite animation.

    When we move, an animation calls that runs a sprite animation but also moves the object in the same direction at a rate where the sprite animation and distance sync.

    Say we move right, I animate the transform to begin at 0,0 and move to 1,0 at the end of the animation.

    I have the player object as a child inside an empty gameobject.
    Our camera follows the empty gameobject.

    The problem is at the end of the animation. The last frame of the animation calls a function that resets the player object to 0,0,0 and updates the parent object to the new WORLD position. Without having the player object inside the empty object, the animation wouldn't work if the player was in a position other than 0,0 and 1,0

    What happens is the camera jerks. For some reason on one frame the gameobject resets to 0,0,0 and then on the next frame the parent object moves. It should be on the same frame so the camera just snaps into the correct position. If I move one frame at a time I see clearly that the two are separate.

    Not sure why. Code snippet is below.

    Is there anyway to force these two things to happen simultaneously? Or maybe skip a camera render frame?

    Thank you for any help,
    Code (CSharp):
    1. gameObject.transform.position = new Vector3(0, 0, 0);
    2. transform.parent.transform.position = GetComponent<PlayerMovement>().currNode.tile.transform.position;
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Wouldn't it be easier to use Root Motion on the player animation instead of reseting/moving by code? Then your camera could track the player and there shouldn't be any jerk.
     
    OgityBogityBoo likes this.
  3. OgityBogityBoo

    OgityBogityBoo

    Joined:
    Dec 9, 2015
    Posts:
    15
    I will go learn more about Root Motion. I've never used it myself so I'm unaware of what I can do with it. Thanks for the tip.
     
  4. OgityBogityBoo

    OgityBogityBoo

    Joined:
    Dec 9, 2015
    Posts:
    15
    I basically followed the steps outlined in Unity's official documentation and in their Authoring Root Motion video, here https://unity3d.com/learn/tutorials/topics/animation/authoring-root-motion
    And it works as you said it would however its moving too far.
    In the animation I am only moving by a value of 1. 0,0,0 to 0,1,0 - but when Root Motion is applied its moving almost 3 times at much. Goes from 0,0,0 to 0,2.97,0 each time.

    No script is affecting movement/animation anymore. I'm simply calling the animation.
    Is it normal for Root Motion to move further than what is noted in the positional animation?
     
  5. OgityBogityBoo

    OgityBogityBoo

    Joined:
    Dec 9, 2015
    Posts:
    15
    All good. Applying Root Motion solved the problem.