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.
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
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?
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!
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 ;) )
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.
And don't forget to write the "f" after 0.5 to make it an official float! ;)
Your answer
Follow this Question
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