Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
This question was closed Dec 27, 2016 at 07:27 PM by wesleywh for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by wesleywh · May 08, 2016 at 05:45 PM · animationspeeddirectionmechanim

C# - Animation Based on Speed and Direction

So I want to play my movement animations for my AI based on that AI's speed and direction. This works when they are going at slower speeds but when I increase to a faster movement speed the animations seem jittery or just don't play(they are stuck in one position of the animation).

The idea is to have a mechanim speed and direction variable that will be used in a blend tree to play the movement animation. So I calculate the speed (this one seems to be fine) and direction (this one does not).

I have two ways of calculating direction: one based on rotation and the other on actual direction (I found this one on the help room):

 void Update () {
         //is moving left or right?
         Vector3 dir = this.transform.position;
         dir.Normalize ();
 //        Debug.Log (dir.x);
         animator.SetFloat ("direction", dir.x);
 //        animator.SetFloat ("direction", transform.rotation.eulerAngles.y - lastRotation);
 //        lastRotation = transform.rotation.eulerAngles.y;

Then the following is the one I am pretty sure is working fine. Its the speed calculation:

             //for speed calculation
         speed = Vector3.Distance(lastPos, this.transform.position) / Time.deltaTime;
         lastPos = this.transform.position;
         switch(lastState) {
             case "calm":
                 animSpeed = speed / calmSpeed;
                 break;
             case "suspicious":
                 animSpeed = speed / suspiciousSpeed;
                 break;
             case "hostile":
                 animSpeed = speed / hostileSpeed;
                 break;
         }
         if (animSpeed > 1000 || animSpeed < -1000 || animSpeed == 0 || 
             animSpeed == Mathf.Infinity || animSpeed == Mathf.NegativeInfinity) {
             animSpeed = 0;
             animator.SetFloat ("speed", animSpeed);
         }
         else {
             animator.SetFloat ("speed", animSpeed);
         }

Then the mechanim is setup like the following:

![alt text][1]

Any ideas as to why the animations might be pausing or not running? It's like they are stuck halfway between starting to run and its idle state (when speed and direction are zero).

Any help and direction on this would be fantastic. [1]: /storage/temp/69670-capture.png

capture.png (46.8 kB)
Comment
Add comment · Show 2
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 wesleywh · May 13, 2016 at 12:08 AM 0
Share

Still nothing? Did I just not explain my issue well enough? Any help on this would be amazing.

avatar image DustinBoyle · Oct 26, 2016 at 10:19 PM 0
Share

Can you please re-post your image of the mechanim setup? I am having the same issue. But the png you posted gets cut off.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by wesleywh · May 17, 2016 at 11:29 PM

Wow I can't believe I did this... feel free to use the above script. It works like a charm! The issue was with the animations themselves. I needed to mark them as loops. So it would play through it once and freeze was my issue. I can't believe it took me this long to figure it out. Hopefully this will help someone in the future.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

93 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 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 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 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 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 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

Animation Issues 0 Answers

Trouble with Velocity speeding things up. 0 Answers

How do I instantiate an objects direction and speed seperately? 1 Answer

Determin movement direction from two vectors accouting for rotation 2 Answers

Help With Network Animations 0 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