Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
2
Question by jjobby · May 14, 2012 at 10:15 AM · movementvelocitynavmeshagent

Is there a way to check agent velocity for NavMeshAgent movement?

I want to check velocity of the moving agent and apply the animation according to its current velocity magnitude (walking, running, idle etc...). Normally, I use rigidbody.velocity but this doesn't work with NavMeshAgent movement. Is there a way to check agent's current velocity for NavMeshAgent movement?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

Answer by syclamoth · May 14, 2012 at 10:37 AM

I just keep track of the transform.position every frame, and use the difference to determine speed.

 private Vector3 previousPosition;

 public float curSpeed;

 void Update()
 {
     Vector3 curMove = transform.position - previousPosition;
     curSpeed = curMove.magnitude / Time.deltaTime;
     previousPosition = transform.position;
 }
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 heywhatsup65 · Apr 25, 2017 at 04:51 AM 0
Share

omg Thank you so very much, I tried doing this exact same thing but in such a messy and complicated way. Upon reading your code and in hindsight it seems incredibly obvious and simple, yet I managed to butcher it! Thank You again! $$anonymous$$y object does not have a Rigidbody so I cant really get the velocity to predict the direction.

avatar image
16

Answer by AlexTudo · Feb 21, 2015 at 08:28 PM

You can also check if (nav.velocity != Vector3.zero) which will show movement if true.

Comment
Add comment · Show 3 · 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 FeenikxFire · Nov 25, 2015 at 07:05 AM 0
Share

Absolutely perfect! To the top you go sir!

avatar image johnCpr · Feb 02, 2018 at 07:33 PM 0
Share

thank you so much, that was perfect

avatar image Aeneas_Ferguson · Dec 17, 2020 at 02:17 AM 0
Share

Thank you so much! You made my life a LOT easier!

avatar image
13

Answer by kristoof · Aug 07, 2017 at 12:59 PM

0-1 velocity:

 float velocity = agent.velocity.magnitude/agent.speed;

i use this for blendtree based animations

Comment
Add comment · Show 2 · 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 Unshackled · Jul 06, 2019 at 10:23 PM 0
Share

This is the best answer for me because you can set what happen at a certain velocity, rather than using it as a bool like the other answers.

avatar image JmLali · Oct 02, 2020 at 08:40 AM 1
Share

agent.velocity.magnitude/agent.speed; <--- Gives you a percentage of the maximum agent speed agent.velocity.magnitude; <--- Gives you the current speed

avatar image
2

Answer by wethecom · Mar 01, 2016 at 08:13 AM

i found this to be a stable alternative finding the predicted speed

float speed = 0f; speed = Vector3.Project(nav.desiredVelocity, transform.forward).magnitude; taken from the unity demo https://unity3d.com/learn/tutorials/projects/stealth/enemy-animation

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
avatar image
1

Answer by pokeyoshi3 · May 31, 2016 at 07:07 PM

 nav.velocity.x 

and

 nav.velocity.z

but the velocity is in world space.

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

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.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

How to make an object moving in a certain direction, move in specific steps like 0.1f 1 Answer

error CS0019: trying to increase velocity when key is pressed twice in quick succession 1 Answer

Stop A player Turning at Specific Point. 1 Answer

How to make a NavMeshAgent move off another gameobject below it? 1 Answer

How to make a gamebject wiggle slowly as it moves forward ? 1 Answer


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