Animate Position from relative previous position (2D)

Hi,

I’m trying to get an object animated by its local position. I’m tring to increase the position.x value +2 everytime Input Fire1 is triggered.

So far I’m not basing it on physics.

I’m trying to simulate a queue, every time I press fire, all the elements are animated 2 units forward, the first one goes away and a new one enters in action.

So what I do is to animate all of them at the same time 2 units.

The approach I’m using is:

Parent Object ( Animator on that one )
|_ Child (Sprite on that one)

I’m setting 2 keyframes, one at the begining and one at the end. The end comes with position.x 0 2

I have two states, idle and movement. Movement contains the animation and its trigger to get in and out by a boolean parameter.

The boolean is set to true on fire1, and to false at the end of the animation by event.

The animation is not set to Loop.

The problem comes when I want to change the value of parent. I added an event to the last keyframe to trigger a function to reset the position of the child and set the new position of the parent. This works fine but I see a ugly jump in the position of the child.

This makes sense as:

1 everyone is in 0.0.0 2 child goes 2.0.0
3 event is trigger 4 change position of parent (now the child goes 2 units forward)
5` change the position of the child (it comes back two units) (even if I dont trigger this change the same happens).

I tries many things, but I cannot get rip of this ugly effect.

Any ideas?? I’d really like to make it work.

I didn’t make it move by physics because of smoothness and problems since im clicking just once…

Any help please.??

How I figured it out:

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.

The solution is here: [RESOLVED] How to make animation position start from last position - Questions & Answers - Unity Discussions

#pragma strict

/**

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