So i have the player,which is a cube and i want it to "jump" to a chosen empty object position.I managed to move the player from it's original place with vector3.MoveTwords(),but in the same time i want to play an animation that shows the cube how it jumps to the empty object position,the problem here is that the empty object position will change so the distance from the cube to te empty object will be different,in this case i need to change the time that it takes the animation to complete,so it does not play and after that let the cube move just in a straight line...I want the animation to take a longer or shorter time to run,giving that the empty object position will always change...
Answer by jjcrawley
Mar 17 at 01:19 AM
Depends what you mean by run longer. If it's playback speed, then you're after the Animator Controller that is playing the animation for that gameobject. Once you have that, it is a simple case of setting the playback speed.
public Animator myAnimator;
//if you haven't assigned one, grab the one on this object
if(myAnimator == null)
myAnimator = GetComponent<Animator>();
void UpdateSpeed(float speed)
myAnimator.speed = speed;
//this would allow you to specify how long you want the animation to play for, not essential but may prove useful. This should allow the speed to be setup so that the animation will end in playTime seconds
void SetSpeed(float animationLength , float playTime)
//speed is a ratio between the actual clip length, and how long you want it to play for.
float speed = animationLength / playTime;
myAnimator.speed = speed;
How you transition in and out of your jump animation is up to you. Don't forget to check the Animator reference docs, may help if speed isn't what you're after. Don't forget to experiment with different things. Also, don't forget to reset the speed when you're finished with it. The playback speed controls the speed of the Animator component, which will affect any animation inside the assigned controller.
Hopefully this helps.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
5 People are following this question.
GameObject from macinim ?
Can I make animations snap to a frame?
Playing an animation attached to another object via C# script?
Animation doesnt play ??
UI animation not working as expected.