• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by saswilson · Jul 11, 2013 at 03:42 PM · animationrelative

Please help, how to set the speed of an animation

Ok, I think the solution to my problem is a simple one, so if someone could help me out and stop me from going mad... All I need to do is to set an animation speed on the player object, relative to the movement of another object(i.e a sphere) So as soon as the sphere starts moving the player animation starts, but at a slow speed, but can increase when the sphere moves faster. And likewise for slowing, the player animation slows, until the sphere has stopped.

I have 1 script on the sphere -

 public static float speed = 300;

 private Vector3 tilt = Vector3.zero;
 private float circ;
 
 void FixedUpdate () 
 {
     
     circ = 2 * Mathf.PI * collider.bounds.extents.x;
     
     tilt.x = -Input.acceleration.y + Input.GetAxis ("Horizontal");
     tilt.z = Input.acceleration.x + Input.GetAxis ("Vertical");
     
     rigidbody.AddForce(tilt * speed * Time.deltaTime);
 }


}

and other on the player. And this is where my animation script will be. So I only want 1 animation at the moment (walking) with the speed of walking to be relative to the movement of the sphere.

Thats it, so please if anyone can help, or to put me in the right direction, I'd appreciate it bigtime! Thanks...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DavidDebnar · Jul 11, 2013 at 03:47 PM

Use AnimationState.speed.

 var sphere : Transform;
 
 function Update () {    
     if(!animation.isPlaying)
         animation.Play("Walk");
  
     animation["Walk"].speed = sphere.rigidbody.velocity.magnitude;
 }


--David--

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image saswilson · Jul 13, 2013 at 01:37 PM 0
Share

thanks for the help.

I don't know if its the best way to do it but I have no Animation component on my player, just an Animator Controller set up with an Idle state and a Walking state.

I nearly have it working adding

anim.SetFloat(hash.speedFloat, ballspeed);

ballspeed = marble.rigidbody.velocity.magnitude;

But the running animation stops the starts again when the sphere is moving, so it looks a bit wrong. Also the speed of the running animation is still not relative to the sphere rotation speed.

Ideally, I need the running animation to stop when the sphere is moving less then say, 0.5 units. So when the sphere is moving less then 0.5, the running animation stops and the Idle animation starts.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to have one animation for multiple objects? 1 Answer

Imported Animations: Make Local? or Other Fix? 2 Answers

Animations Relative to World Space 1 Answer

Can the animation editor create local rotational data? 3 Answers

Adding animation clips via script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges