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
0
Question by mohghazala96 · Sep 21, 2016 at 06:27 PM · transformcoroutineupdatedistance check

inconsistent results calculating the distance moved by an object

the results are inaccurate the the first print function always prints 6 but the second one in the WaitAndPrint method outputs inconsistent values once it is 13.54 once it's 13.4 . 13.38 and so on it never give a fixed value. And it is so essential to get the accurate distance the object moves in one second.

    private float speed;


 void Start(){

  speed = 7.25f;
     init();
  }


     public void init(){


 
  print ("before"+transform.position.z);
  StartCoroutine (WaitAndPrint ());
    }

  IEnumerator WaitAndPrint() {

      yield return new WaitForSeconds(1f);
  print ("after"+transform.position.z  );
  }


     void Update() { 

      transform.Translate (Vector3.forward * Time.deltaTime * speed);

      }



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

Answer by tanoshimi · Sep 21, 2016 at 06:46 PM

This is to be expected. Your game does not run at a fixed frame rate. WaitForSeconds means "wait for this amount of time and then continue execution in the next frame", so you're also including the variable frame execution time in your timing.

But there's an architectural problem here - I can't think of many games where a distance error of 0.15% should matter - and, if it does for you, you might have to rethink your approach.

Comment
Add comment · Show 13 · 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 mohghazala96 · Sep 21, 2016 at 07:04 PM 0
Share

can i fix the frame rate? i tried Application.targetFrameRate = 60; but i was getting the same results Or can i know the true distance the transform.translate method will move the object without using code. Yes it does as this i want the value to be 13.5 if it's below it will be impossible to pass the level if higher the level will be extremely easy.

avatar image tanoshimi mohghazala96 · Sep 21, 2016 at 07:08 PM 0
Share

No, you can set a fixed target frame rate. But that is just a target for the application to aim for. If you try to do calculations in Update() that take 20ms to execute, for example, it's simply impossible to maintain 60fps.

And you can't know the "true" distance moved by translate, since that varies each frame too.

avatar image mohghazala96 · Sep 21, 2016 at 07:12 PM 0
Share

thanks for your help :D , is there any way i could get around this as i said earlier the distance moved is the main core deter$$anonymous$$ing the difficulty so i cant just proceed in the fashion.

avatar image Bonfire-Boy mohghazala96 · Sep 21, 2016 at 07:46 PM 0
Share

It's not clear to me what you're trying to do.

If you want the distance moved since the last frame, you can cache the position each frame and work it out.

If you want the distance moved in the next frame, you need to fix the distance rather than the speed, since you can't know how long the next frame's going to take.

Or you could just fix the distance you move the thing each frame (ie don't multiply by time.deltaTime which varies out of your control). Then you'd know the distance moved each frame because you'd be setting it directly using variables you have total control over, but you'd lose the smoothness of the motion. You can't have both - smooth motion means dealing with varying and unpredictable frame rate.

avatar image mohghazala96 Bonfire-Boy · Sep 21, 2016 at 08:02 PM 0
Share

i want to get the distance moved since the last frame.when i cache the positions i dont get an accurate value as well. my main problem is that the main object which i want to move has a child object which jumps in a time of 0.88 seconds so i want to know in the 0.88 seconds how far my main object moved.

Show more comments
Show more comments

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

Please help. I can't stop this light from moving!!! 1 Answer

using coroutines for move objects - performance. 1 Answer

Flash image while ammo is low - coroutines? 3 Answers

How to Update a GameObject coordinates to folow in realtime and don't lose performens or crushing Unity.[SOLVED] 1 Answer

Coroutine in Editor? 7 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