Wednesday, June 20, 2018

unity - Use an object's x position to seek within an animation


I have an animation clip I want to play, using the object's x position to control which frame of the animation is displayed. ie.



  • When the object is at x = -2, the animation should show its initial frame.

  • As the object moves rightward (increasing x) the animation should play forward.

  • When the object is at x = 6, the animation should show its final frame.

  • As the object moves leftward (decreasing x) between these values, the animation should play in reverse.


I thinks this is a form of clamping where I want to map x values into key frames of the animation.



Below just a simple code to run animation:


void Update () {
if (isPositionChanging)
{
if(transform.position.x > xPos){
if(incrementOrDecrement <=12) {
incrementOrDecrement ++;
AnimationPlayOnPositionValue(1, incrementOrDecrement);
}
}

else {
if (incrementOrDecrement >= 0)
{
incrementOrDecrement--;
AnimationPlayOnPositionValue(1, incrementOrDecrement);
}
}

}
}


void AnimationPlayOnPositionValue(float speed,float timeToUpdate) {
animatedObject["MachineAnimationComplete"].speed = speed;
animatedObject["MachineAnimationComplete"].time = timeToUpdate;
animatedObject.Play();

}


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...