Anim object with Relative Position
Hello !
I want to make an animation when my 'chess pieces' ( is not chess piece but it is for the image ) moove. The better way is to create parent empty gameobject for each piece and assign this nice script ? http://stackoverflow.com/questions/25672956/unity3d-how-to-make-animation-position-start-from-last-position **/
/*
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;
}
}
My request is simple but this solution is too ugly.. Any suggestions ?
Thank's in advance !
Comment
Your answer
Follow this Question
Related Questions
Convert int to binary 1 Answer
Trying to change an animation with a UI button 2 Answers
C# GetKeyDown not working 3 Answers
c# sound in unity 5 1 Answer
AnimationComplete() Error 0 Answers