- Home /
[RESOLVED] How to make animation position start from last position
I want to use the unity animations to move an object from A to B everytime I trigger fire1. but the problem is that next time it will start from the begining.
further explanation : http://forum.unity3d.com/threads/animate-position-from-relative-previous-position-2d.265847/
Answer by Knskan3 · Sep 04, 2014 at 12:32 PM
Problem Solved
Unity Animation relative to last position without looping
This code solves the problem regarding animations based on last relative position. In this example, each time we press Fire1 a box will be animated to move from X = 0 to X = 10 Using the animation will help us to provide richer transitions and smooth movements based on curves without the problem of looping.
The idea is to have the animated object inside an empty parent so the animation of the object will be based into local position.
When the animation is finished we update the object and its parent location to match in the last position.
If you have any doubts please ask.
#pragma strict
/**
* Animation in Unity Relative to last position without looping
* @autor Knskank3
* http://stackoverflow.com/users/1287772/knskan3
* 04/09/2014
**/
/*
This code solves the problem regarding animations based on last relative ImagePosition
In this example, each time we press Fire1 a box will be animated to move from X = 0 to X = 10
Using the animation will help us to provide richer transitions and smooth movements based on curves
without the problem of looping
*/
// This var will determine if the animation is started
public var animation_started : boolean = false;
// This var will determine if the animation is finished
public var animation_finished : boolean = true;
function Update () {
// if user triggers Fire1
if( Input.GetButtonUp('Fire1')){
// initialize the flags
animation_started = true;
animation_finished = false;
// Start the animation
// this animation moves the box from local X = 0 to X = 10 using a curve to deaccelerate
animation.Play("boxanim");
}
}
/* This function is trigger at the end of the animation */
public function animationFinished() : void {
animation_finished = true;
}
/*
At the end of the frame if the animation is finished
we update the position of the parent to the last position of the child
and set the position of the child to zero inside the parent.
*/
function LateUpdate () {
// if the animation is finished and it was started
if(animation_finished && animation_started) {
// set the flag
animation_started = false;
// update the parent position
transform.parent.position = transform.position;
// update the box position to zero inside the parent
transform.localPosition = Vector3.zero;
}
}
Animator should not be attached to the parent object. Parent should not have any animation component attached to it. Whatever you want to animate, it should be within that parent. Also, try re animating the child object if the existing animations still play in the initial position.
P.S. Use the comments section to clarify.
Imagine I have I character that attacks. If you press fire1 you step forward and kick. The next time I press fire again I want the character to be placed where it was at the end of the step.
What I want:
idle[ x = 0 ] --> --> press Fire1 [ x = 1] --> idle [x = 1] ( remains in x = 1 not x=0) --> press Fire1 [x = 2]
This way the character is moving forward by animations. not by moving the position by any other way.
How and where do I trigger the function animationFinished() at the end of the animation? Can you please give an example.
Thank you in advance.
You have to add this call in the last animation frame. Here you have a picture: http://forum.unity3d.com/attachments/animationevent-png.119387
I kind of follow this but am failing to get it implemented probably due to the setup of the prefab that is beyond my control to modify which has this layout:
ParentGameObject (has an AnimationController attached) ->GameObject_LOD00 ->GameObject_origin -->Root
When I run the scene the animations do what this thread describes as in reverting to starting position ins$$anonymous$$d of remaining in place
I an empty GameObject and place the above into it, so now the parent does not have the animation controller, but I still get the same unwanted behaviour.
But as the script you supply is attached to the new empty GameObject the events in the animation controller are orphaned and so flag up as not assigned?
Answer by AndreBR · Jan 14, 2016 at 11:43 AM
I solved by: - Accessing the Animator interface, - clicking on previous animation state, - clicking on "Add Behavior" button and creating the following script:
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
animator.transform.parent.gameObject.transform.position = animator.transform.position;
}
You can simplify that statement like this:
animator.transform.parent.position = animator.transform.position;
Best solution!
On a side note: For those who like to utilize "root motion" for animations that manipulate RectTransform values. Nope, that won't work. That feature only works with transform components :( (Well, at least not in Unity 2017.1).
Best answer! I have never understood those behaviors. Thanks!
This doesn´t Work for me. :..( Always show me a NullReferenceException.
Help
!
Answer by GluedBrain · Sep 04, 2014 at 11:09 AM
First of all make sure you have applied the Animator component to the child object. If the object that you want to animate is the parent object, then create an empty gameobject and place your object with animator component inside this empty object. Secondly, you got to uncheck the apply root motion checkbox of the animator component.
Answer by Okiski · Feb 22, 2018 at 02:58 PM
Create an empty animator object (my empty's name is "New State") and use that script
Animator anim;
anim = GetComponent<Animator>();
public void AnimationFinished()
{
anim.Play("New State");
transform.parent.position = transform.position;
transform.localPosition = Vector3.zero;
}
and then this function will be in the end of the animation (you can do it from here https://forum.unity.com/attachments/animationevent-png.119387/ ) and that s it.
dont forget this
anim = GetComponent<Animator>();
Your answer
![](https://koobas.hobune.stream/wayback/20220613162240im_/https://answers.unity.com/themes/thub/images/avi.jpg)