0
如何在Unity中相對於最後位置實現動畫。通常情況下,在Unity中實現動畫時,每個循環都從相同的位置開始。我想實現一個動畫相對於動畫對象的最後位置,以避免使用力或數學計算。Unity3d。如何使動畫位置從最後位置開始
如何在Unity中相對於最後位置實現動畫。通常情況下,在Unity中實現動畫時,每個循環都從相同的位置開始。我想實現一個動畫相對於動畫對象的最後位置,以避免使用力或數學計算。Unity3d。如何使動畫位置從最後位置開始
如何我想通了:
團結動畫相對於最後一個位置不循環
這個代碼就解決了基於上次的相對位置的動畫問題。在這個例子中,每當我們按下Fire1時,一個盒子就會動畫從X = 0移動到X = 10。使用動畫將幫助我們提供更豐富的過渡和基於曲線的平滑移動,而不會出現循環問題。
這個想法是讓動畫對象在一個空的父對象內,這樣對象的動畫將會基於本地位置。
當動畫結束時,我們更新對象及其父位置以匹配上一個位置。
如果您有任何疑問,請詢問。
#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;
}
}