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 /
  • Help Room /
avatar image
0
Question by HIJE1999 · Mar 30, 2017 at 08:39 PM · transformpositionspaceorbit

Why isn't this code working?

I'm trying for the time to be output when the gameobject (earth) has returned to its original position - aka the orbital period.

The code is below:

 Text textbox;
 private Vector3 initialposition;
 private float timeperiod = 0;
 private Vector3 trailpos;

 // Use this for initialization

 void Start () {
       var earth = GameObject.Find("Earth");
     initialposition = earth.transform.position;
     textbox = GetComponent<Text>();
 }
 
 // Update is called once per frame
 void Update () {
     var earth = GameObject.Find("Earth");
     if (earth.transform.position == initialposition)
     {
         timeperiod = Time.realtimeSinceStartup - timeperiod;
         textbox.text += "Orbital Period: " + timeperiod.ToString() + "\n";
     }
 }


I don't see what I'm doing wrong - or why there's an output ONLY once after around 1 second has elapsed.

Can anyone see what I'm doing wrong? Thank you in advance.

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 gameplay4all · Mar 30, 2017 at 09:19 PM

The earth will never be at exactly the same place as it has been before, this is because the frame-rate varies a lot in a game and so the timestep (I guess you are using Time.deltaTime?) is never consistent and therefore it won't end up at exactly the same starting position as it did before.

That's the easy explanation, but actually this is a common new-programmer's mistake: floats (a Vector3 is made up of 3 floats) are saved in a tricky way in memory that gives them rounding errors. So sometimes you think all your objects have x-coordinate at 3, but actually it is 3.000000001 or something.

Long story short: don't use equality checks with floats (use integers for that) lower-than and higher-than checks are fine.

As for a solution, you can try using Vector3.Distance(earth.transform.position, initialposition) to check if it is in a certain range of the starting point (This will be true for multiple frames!)

I hope you learned some things :)

-Gameplay4all

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 HIJE1999 · Mar 30, 2017 at 09:52 PM 0
Share

Thank you so much for the quick response, and yes this all makes a lot more sense - you also guessed right.. I am a new programmer :)

UPDATE: Ok, I've implemented your solution and it's doing nothing :D

This is the updated code:

 Text textbox;
 private Vector3 initialposition;
 private float timeperiod = 0;
 private float distance;

 // Use this for initialization

 void Start () {
       var earth = GameObject.Find("Earth");
     initialposition = earth.transform.position;
     textbox = GetComponent<Text>();
 }
 
 // Update is called once per frame
 void Update () {
     var earth = GameObject.Find("Earth");
    distance = (Vector3.Distance(earth.transform.position, initialposition));
     if (distance < 0.5)
     {
         timeperiod = Time.realtimeSinceStartup - timeperiod;
         textbox.text += "Orbital Period: " + timeperiod.ToString() + "\n";
     }
 }

}

Attached is the resultant orbit - like before there's a result at around 1 second but there's no result when it orbits around once.... Any ideas? alt text

unityhelp.png (183.5 kB)
avatar image gameplay4all HIJE1999 · Mar 31, 2017 at 03:29 PM 0
Share

Hey, here I am again :)

You should set the textbox.text and not append the text because now it will get bigger and bigger and only the first words are shown and the later words are cropped out. So ins$$anonymous$$d of textbox.text += ... use textbox.text = ...

One last tip I might give you is too look at the orbital movement code, most likely it uses a Sin and Cos function. Generally the x coordinate is described by the function x = a * $$anonymous$$athf.Cos(2 * $$anonymous$$athf.PI * f) and the y coordinate is described by y = b * $$anonymous$$athf.Sin(2 * $$anonymous$$athf.PI * f) f is the frequency of the periodic movement and the period is T = 1 / f. a and b are the radius of the orbit, you can use different values to make elliptical orbits. You might already know all of this, but because of your method to deter$$anonymous$$e the orbital period I guess you might be using third-party code. So look for a Sin or Cos function, or better yet: write your own script! :)

Good luck with your project!

avatar image gameplay4all · Mar 30, 2017 at 09:58 PM 0
Share

You should look out for the little details in the scripting reference :) For example, if you look up Vector3.Distance you'll notice that it has a "return type" of float. So you can basically see the whole block of code Vector3.Distance(earth.transform.position, initialposition) as just a float, like 2.3f

So you can directly use if(Vector3.Distance(...) < 0.5f){...}

Another thing that you might notice on the (c#) scripting reference is that float.Parse takes an "argument" of type string. So that's used for converting strings, pieces of text, to floats. And you tried giving it a float ins$$anonymous$$d of string ;)

So just put Vector3.Distance inside the if statement and it should be fine.

(I typed this on mobile so excuse the typos and it's a bit buggy so I might not comment in the right place ;) )

avatar image HIJE1999 gameplay4all · Mar 30, 2017 at 10:29 PM 0
Share

Hey, thanks again. You're really good at this :) (I think you answered my response before I updated it- with the converting correction that meant there were no errors).

However if you do see my updated response above, it still doesn't work so I don't know what to do :'D.

avatar image gameplay4all · Mar 30, 2017 at 10:01 PM 0
Share

And don't forget to write the "f" after 0.5 to make it an official float! ;)

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

111 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do you access the transform of a gameobject and put it in a variable? 3 Answers

how to move the enemy in the current position of the player but only ones? 0 Answers

Camera isn't move position? Why my camera isn't change position? 0 Answers

I'm making a simple platformer and I want the position to reset when you fall off the screen 2 Answers

Position of empty game objects 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