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 /
avatar image
3
Question by Narven · Apr 27, 2010 at 06:24 AM · timedistance

Calculate the distance a object has moved.

Hi,

I'm trying to calculate the distance a object has moved to be able do decrease fuel (in a vehicle).

Or is best to calculate de overall time the object has been moving??

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

3 Replies

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

Answer by StephanK · Apr 27, 2010 at 06:30 AM

float distanceTravelled = 0; Vector3 lastPosition;

void Start() { lastPosition = transform.position; }

void Update() { distanceTravelled += Vector3.Distance(transform.position, lastPosition); lastPosition = transform.position; }

Comment
Add comment · Show 9 · 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 Narven · Apr 27, 2010 at 06:59 AM 0
Share

That works, but inicialy, it calculates de distance based on the inicial position of the object, and gives a wrong calculation.

avatar image Narven · Apr 27, 2010 at 07:00 AM 0
Share

Based on my respawn... is giving already 160

avatar image StephanK · Apr 27, 2010 at 06:01 PM 0
Share

I edited the code...

avatar image wenhua · Feb 02, 2012 at 09:55 AM 1
Share

any1 can convert it to javascript ^^

for the Vector3 lastPosition;

avatar image Bunny83 · Jan 22, 2016 at 11:17 AM 1
Share

@John3D It is in "world units" like everything in Unity. It could be interpreted as meter, yes, but Unity itself doesn't have a unit for length.

Show more comments
avatar image
0

Answer by v0nni · Jul 09, 2012 at 02:01 PM

Here is how i converted the entire code to javascript

private var distanceTravelled : float= 0; private var lastPosition:Vector3; function Start() { lastPosition = transform.position; } function Update() { distanceTravelled += Vector3.Distance(transform.position, lastPosition); lastPosition = 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
0

Answer by BawsAnimations · Sep 15, 2013 at 08:33 PM

Script Conversion to Javascript :

 var DistanceTravelled : float = 0;
 var lastPosition : Vector3;
 
 function Start()
 {
 //cached data.
 lastPosition = transform.position;
 }
 
 function Update()
 {
 DistanceTravelled += Vector3.Distance(transform.position, lastPosition);
 
 lastPosition = 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

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

10 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

Related Questions

Calculating player distance meter? C# Unity2D 1 Answer

How can I get the Time taken for the 2 moving objects to touch each other 2 Answers

Destroy instances after set time 1 Answer

Creating an equation to simulate time based on rate and distance. 0 Answers

Time a ball is thrown in the air? 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