Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
3
Question by Mattivc · May 11, 2010 at 02:21 PM · gameobjectphysicsprogrammingspeed

Getting the speed of a gameobject

How can i by using javscript get the speed of a gameobject? I cant use Rigidbody.velocity as the object is not a Rigidbody.

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

7 Replies

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

Answer by qJake · May 11, 2010 at 10:10 PM

I just wrote this now, I didn't test it or anything, but you can give it a try if you want. Just drop this into a file called Speed.cs and you can read the public "speed" variable from it to get the speed of the object.

using UnityEngine;

public class Speed : MonoBehaviour { public float speed = 0;

 Vector3 lastPosition = Vector3.zero;

 void FixedUpdate()
 {
     speed = (transform.position - lastPosition).magnitude;
     lastPosition = transform.position;
 }

}

Comment
Add comment · Show 6 · 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 Mattivc · May 12, 2010 at 01:17 PM 0
Share

I tryed this, but the output keep fluctuating betwene the actual speed value and zero.

avatar image qJake · May 12, 2010 at 09:25 PM 0
Share

I changed Update to FixedUpdate, try that.

avatar image jorjdboss · Jan 08, 2014 at 09:44 AM 0
Share

Its better to use a coroutine for this task. Please look at my answer.

avatar image Krynin · Mar 15, 2018 at 07:04 PM 0
Share

It may be a golden shovel award and noob question but how can I replace 2D text with the value from that script??

avatar image lwks Krynin · May 23, 2019 at 01:16 PM 0
Share

Well you should've probably created another question, but you'd have to make something like this

 using UnityEngine.UI;
 
 public Text screenText;
 public float value;
 
 void Start()
 {
 screenText.text = value.ToString();
 }
avatar image Pangamini · May 23, 2019 at 02:20 PM 1
Share

to get a real speed, you should do speed = (transform.position - lastPosition).magnitude / Time.fixedDeltaTime

Optionally, you can call it 'rawSpeed' and calculate some 'smoothSpeed' in Update() by using SmoothDamp()

avatar image
3

Answer by jorjdboss · Feb 29, 2012 at 04:23 PM

Its hardly 8-9 lines of code. Please check out this link to my blog: http://fungamesprogrammer.blogspot.in/2012/02/calculate-velocity-of-non-rigidbodies.html

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 FullHeartGames · Oct 02, 2016 at 08:10 PM 0
Share

I like this method alot!

avatar image MFKJ · Sep 11, 2018 at 04:49 AM 0
Share

Your and @qJake answer is not same!

avatar image
3

Answer by JamesL98 · May 26, 2019 at 07:50 AM

Hey all I know this is an old thread but ive been wandering the internet for a few hours trying to find a solution and the closest I got was jorjdboss' response, however it still wasnt working but he was on the right track. Here is the code I modified for any lost puppy wandering the internet like i was (working in 2019.1.2f1). This responds the speed in km/h and rounds it to the nearest whole number and doesn't fluctuate between a value and zero like most of the solutions i was finding. Hope this helps someone.

 void Start()
     {
         StartCoroutine(CalcVelocity());
     }
 
     IEnumerator CalcVelocity()
     {
         while(Application.isPlaying)
         {
             lastPos = player.transform.position;
             yield return new WaitForFixedUpdate();
             movesSpeed = Mathf.RoundToInt(Vector3.Distance(player.transform.position,lastPos)/Time.fixedDeltaTime);
         }
         
     }
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
1

Answer by ankur30884_unclaimed · Jun 07, 2012 at 10:48 AM

calculate speed of object in KM/H

Vector3 lastPosition = Vector3.zero;

 void FixedUpdate()
 {
     dispSpeed = (((transform.position - lastPosition).magnitude) / Time.deltaTime) ;
     lastPosition = transform.position;
 }
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 MichaelJT · Apr 06, 2015 at 04:20 PM 0
Share

Tested and it doesn't work.

avatar image Pangamini · May 23, 2019 at 02:21 PM 0
Share

yeah, use Time.fixedDeltaTIme in FixedUpdate()

avatar image
0

Answer by davedev · May 11, 2010 at 02:34 PM

This might not be the best, official, easiest way, but in an effort to respond first, here's how I've done it one of my projects.

Basically, you get/save the loc from the previous update and then use the difference in distance and time since the previous update to calculate the time:

// determine the amount of time since last update

var curTime = Time.time;

var updateTime = curTime - lastUpdateTime;

// save old loc at beginning of your update loop

var oldLoc = transform.position;

// do something to change the loc on this update

var newLoc = transform.position; var updateDistanceMeters = Vector3.Distance(newLoc, oldLoc);

// my results are in miles per hour var updateDistanceMiles = convertMetersToMiles(updateDistanceMeters);

var updateTimeHours = updateTime/60.0/60.0; calculatedAircraftSpeed = (updateDistanceMiles)/(updateTimeHours);

// save the current time for use on the next update

lastUpdateTime = curTime;

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 Mattivc · May 11, 2010 at 09:34 PM 0
Share

Im having trouble folowing this code, do any one have a suggestion for a more simple solution for this problem?

avatar image qJake · May 11, 2010 at 10:09 PM 0
Share

When you post code, be sure to either surround it with tags, or hilight it and click the Code button in the toolbar.

  • 1
  • 2
  • ›

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

15 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

Related Questions

Multiple Cars not working 1 Answer

how to instantly check if two colliders overlaying? 2 Answers

I am trying to rotate one of a few 3d models by click and dragging the mouse what code would that be? or how would I be able to do that? 1 Answer

GameObject Static & Active Flags 1 Answer

Having trouble implementing drag force 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