Hello. I don’t know am I experiencing limitation of C# or Unity. Basically, this code below should return maximum h == l and then, at some point, it should become 0 again… So…it goes all the way up to L, but if I test “if (h == )” it doesn’t return true. Same with h == 0. I assume there is little 0.00x going on here, but even Function Mathf.Approximately returns false for (h, 0), but true for (h, L). I’ve tried using doubles, but still nothing. Thing that bothers me is that in unity editor, it shows no decimals.
In theory, this code should hit exactly L and 0, but it doesn’t. I am wondering if there is an way of telling the program to store 1/3 instead 0.333… (this is just an example). Because 0.333 * 3 != 3 and 1/3 * 3 == 3.
//Calculates gravity and the velocity for (t = 0)
//based on given max jump height and time needed to reach it
public float l, t;
float h, v, a;
Start () {
a = l * 2 / (t + (t + 1));
v = a * t;
}
FixedUpdate () {
h += v;
v -= a;
}
I’ve recoded your script to make it more readable (I doubt you’ll get much help with a script containing all single char variables :p)
public float length, time;
float height, velocity, accel;
void Start () {
accel = length * 2 / (time+(time+1)); // The inner parenthesis do nothing, was this a mistake?
velocity = accel * time;
}
//Use Update with *Time.deltaTime to get more stable results. FixedUpdate is slower, for physics objects
void Update () {
height += velocity * Time.deltaTime;
velocity -= accel * Time.deltaTime;
Debug.Log("Height: "+height+", Time: "+(Time.timeSinceLevelLoad));
}
So few things: Testing (h == anything) will always be false, because these numbers are all floats, and will most likely be 3.523445 instead of the (h==3) you might want. Instead try (h>l) or (h<=0).
Secondly, “0.333 * 3 != 3 and 1/3 * 3 == 3” I think you meant != 1, and == 1. But regardless…
1/3 * 3 == 1, yes. But 0.3333333333333333 * 3 == 1 as well. If you type 1/3 in a calculator you will get 0.3333333333333333. There is no such thing as putting “1/3” as a number in code.
Mathf.Approximately will fix 0.000000001 into 0, yes. But in your case the number goes from 0.1234453 to -0.4234523. It would be much better to use something like if(Mathf.Round(h) == 0), to turn the 0.1234452 into 0.
Lastly, your algorithm doesn’t seem to get the desired precise jumping you want (I put in 5 max height over 2 seconds, but it only reached a height of 4). Using the code I supplied with the debug log, it should help refine it.
Thank you for the quick response. I’ve used FixedUpdate and slower time to catch every frame by eye, and only without using deltaTime, in theory and in Unity, the code reaches “h”, but the thing is that no code can detect it even if I am using doubles instead of floats. My game relays on precision and I’ve thought of using Mathf.MoveTowards with accelerating speed to do this kind of job.
Any suggestions on how to create precise jumping code with a=const, and given height that will be reached exactly at some point?
EDIT:
This link was extremly useful for making precise jump (Tested in GamMaker), but still no answer how to float problem. It showed exact h (h = 2 (round number ofc.)), but as I compared it with same number, it returned false. Should I compare float 2 to 2.0 or 2f instad of 2?
Maybe someone can explain how unity (or C#) sees plain written number (not declared variables)?