[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/

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;
	}
}

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;

}

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.

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>();