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
0
Question by ratboy · Apr 19, 2012 at 11:30 PM · gameobjecttimedistance

Distance an object has travelled over a certain time?

Hi, i have a physics game, and in order for the loseGame condition to work, I need to detect if a game object has moved less than a specified distance over a specified time. can anybody point me in the right direction?

cheers.

ie: check how far the object has travelled in the last 'x' seconds, if it is less than 'y' units, do something.

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

1 Reply

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

Answer by Fabkins · Apr 20, 2012 at 12:02 AM

I cant think of a straight forward way of doing this.

First you will want to work out the distance since the last update. So something like:

 var distance: float=0;
 
 function Update()
 {
 distance += Vector3.Distance(object.position, previousPosition);
 previousPosition=object.Position;
 }

Now what not clear to me is whether you wanting the player to have moved a total amount of time since he started or he has moved a at least x amount distance in the last y seconds.

If its the former then the above gives you everything you want. If its the later then you going to need something more elaberate.

You will need to sample a distance in time and work out what the distance is for the last number of samples.

So maybe something like:

     var object: GameObject;
     
     var NumberOfSamples: int=10;
     var distances: float[];
     var lastSampleSlot: int=0;
     var previousPosition: Vector3;
     
     function Start()
     {
         distances=new float[NumberOfSamples];
     }
     
     function Update()
     {
         var currentSampleSlot: int = Time.time % NumberOfSamples;
         if( currentSampleSlot != lastSampleSlot)
         {
              distances[ currentSampleSlot] =0;
         }
         
         distances[ currentSampleSlot ] += Vector3.Distance(object.transform.position, previousPosition);
         previousPosition=object.transform.position;
         
         // sum all the values in the distances array to work out the distance in the last x samples secs.
         
         var TotalSampleDistance: float =0;
         for (var value: float in distances) 
         {        
             TotalSampleDistance += value;
         }
     }

For any given second, it works how far you have travelled in that second. It records that in the array. Everytime you move into a new slot, you clear out whatever was in there before.

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 ratboy · Apr 20, 2012 at 12:23 AM 0
Share

getting a 'insert semi colon' error on the var distances: float[no of samples]; ? is this a problem with the array?

avatar image ratboy · Apr 20, 2012 at 12:31 AM 0
Share

and it is the latter, i need to work out how far it HAS travelled..

avatar image Fabkins · Apr 20, 2012 at 12:39 AM 0
Share

Sorry I was trying to give the impression of what was required and it was a bit sloppy. I've updated the example so it should at least compile now. Not tried it running.

avatar image ratboy · Apr 20, 2012 at 12:49 AM 0
Share

haha i was wondering why it wasn't working, you've written function Update as 'update' so it wasn't calling - works a treat, thanks for the quick and good answer! :)

avatar image Fabkins · Apr 20, 2012 at 12:53 AM 0
Share

You're welcome.

By the way, if you want to change the period you sample over, you can put a factor on time. ie

    var factor: float = 10;
    var currentSampleSlot: int = (Time.time * factor) % NumberOfSamples;

Should take ten 1 millisecond samples. So distance in 1 sec. Conversally a factor of 0.1 will be 100 seconds.

Again, not tried it but it reasons in my head :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to delay a line of code. 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity3D Timer. 1 Answer

Unity3D Game Time 1 Answer

grab all game objects with a distance of the object? 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