Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by MythStrott · Aug 03, 2010 at 01:00 AM · animationjavascriptobjectspeed

Current speed of an object?

I'm scripting the animation for my character.

How can I check the speed of the character?

My code:

animation.Stop(); animation.Play("Idle");

function Update () {

if (INSERT_SPEED_VARIABLE_HERE) > 0.1) { animation.CrossFade("WalkCycle"); } }

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

5 Replies

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

Answer by boribhi · Aug 03, 2010 at 01:33 AM

if your character has a rigidbody, try:

var vel = rigidbody.velocity;      //to get a Vector3 representation of the velocity

or

var vel = rigidbody.velocity;      //to get a Vector3 representation of the velocity
speed = vel.magnitude;             // to get magnitude

Comment
Add comment · Show 5 · 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 MythStrott · Aug 03, 2010 at 02:13 AM 0
Share

I don't have a rigidbody. Should I?

avatar image · Aug 03, 2010 at 06:30 AM 0
Share

How are you changing the position of your character? Are you adding force, or just setting the transform.position each frame?

avatar image boribhi · Aug 04, 2010 at 11:11 AM 0
Share

Why not trying to add a rigidbody? if you have not to add one, tell us what your character consists of.

avatar image Rennat · Dec 02, 2010 at 07:54 PM 0
Share

definitely don't start using the physics engine just to know how fast your character is moving :) but if you're already using it then this is the way to go.

avatar image PanzerPotato · Oct 28, 2017 at 05:50 PM 0
Share

I'm applying this to the vehicle I'm making, but it still gives me a positive velocity when I'm driving in reverse. How do I fix this?

avatar image
6

Answer by trapazza · Apr 02, 2013 at 02:27 PM

In case you only have their transforms:

 speed = total distance traveled / total time taken

or something like:

 float speed = (transform.position - this.mLastPosition).magnitude / elapsedTime;
 this.mLastPosition = transform.position;
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
5

Answer by Caps · Dec 02, 2010 at 07:23 PM

if youre using character controller:

http://unity3d.com/support/documentation/ScriptReference/CharacterController-velocity.html

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 NSO · Apr 11, 2015 at 06:52 PM

var vel = rigidbody.velocity; This will get your current velocity in x, y and z not your current speed. If you are traveling with the current velocity (60, 0, 0) it is correct and can be used but NOT with the velocity (60, 60, 0). I say how to get the speed of a rigidbody when i solved the problem cant find any clues on the big net.

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 DCrosby · Jun 24, 2016 at 04:50 PM 1
Share

var vel = rigidbody.velocity.magnitude

avatar image eamendoza · Jul 24, 2017 at 06:53 AM 0
Share

or, if you want just the speed on a plane (for example, ground speed) use Pythagoras to deter$$anonymous$$e the magnitude across the x-z axes

avatar image
0

Answer by amitDklein · Sep 15, 2020 at 08:17 AM

You can use Transform.hasChanged

         if (!this.transform.hasChanged)
         {
             print("Player is not moving");
         }
         transform.hasChanged = false;

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 Hellium · Sep 15, 2020 at 09:04 AM 0
Share

So setting, for instance, transform.position will always set hasChanged on the transform, regardless of there being any actual change.

So even if the flag is true, it does not mean the object is moving.

Other examples: object rotates, object is scaled, parent is changed

avatar image amitDklein Hellium · Sep 15, 2020 at 06:45 PM 0
Share

For this example, and many others, this is the most simple solution.

Any way, if the object rotates, scaled, or parent is changed the object will be kind of moving...

$$anonymous$$ost of the places I know they check for if object not moving will be best using this method.

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

8 People are following this question.

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

Related Questions

JavaScript and Ajax second addition Tutorial 11 case problem 2 0 Answers

Desactive a object 1 Answer

Animation play on input, else play idle animation. 2 Answers

Walking animation problem. 0 Answers

Enemy Attack Animation 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