#`moveSpeed Time deltaTime Vector3 left`
1 messages · Page 1 of 1 (latest)
im sorry to bother again but i put it through the system and it did fix things, however when i compare my original code and the optimized code they should both equal to the same value (without bodmas) but they dont
does c# use bodmas?
yes but the code starts of with transform.position + movespeed compared to transform.position + vector3.left
//0,0,0 = 0,0,0 + -1,0,0 * 1 * 1 = -1,0,0 this is the original
//0,0,0 = 0,0,0 + 1 * 1 + -1,0,0 = 0,0,0 this is the optimized
Why do you have two addition operations in the second one
show your code
void Movement()
{
transform.position += moveSpeed * Time.deltaTime * Vector3.left;
}
and the old code?
void Movement()
{
transform.position += Vector3.left * moveSpeed * Time.deltaTime;
}
hmm
what are you seeing that's different?
the old code would run as -1,0,0 by the end whereas the new one would end as 0,0,0
Is this something you're observing or something you're guessing?
wdym by "the end"?
The end of what
i just turned every variable and text into hard numbers and calculated it by myself
it does behave virtually if not exactly the same but i still dont understand
what happens when you add moveSpeed to transform.position?
by itself if movespeed was 1 and transform.position was 1,1,1
how would c# calculate that?
here's the calculations:
for Vector3.left * speed * Time.deltaTime:
1. (-1, 0, 0) * speed = (-speed, 0, 0)
2. (speed, 0, 0) * dt = (-speed * dt, 0, 0)
for speed * dt * V3.left:
1. speed * dt = speed * dt (let's call this x)
2. x * (-1, 0, 0) = (-x, 0, 0)```
and since x is speed * dt, then -x is -speed * dt, aka the same as the first result
or with hard nuumbers:
let's just say dt is .5 and speed is 10:
(-1, 0, 0) * 10 = (-10, 0, 0)
(10, 0, 0) * .5 = (-5, 0, 0)
10 * .5 = 5
5 * (-1, 0, 0) = (-5, 0, 0)```
hm alright but what happened to the addition?
the addition happens at the end
it calculates all this then adds it to transform.position
and assigns it back to transform.position
transform.position += Vector3.left * speed * Time.deltaTime;
is the same as
transform.position = transform.position + (Vector3.left * speed * Time.deltaTime);
note the parentheses
so is it when i use += that it first does the other things before adding it?
x += y is just a short way to write x = x + y
so like...
calculating what y is has to happen before the addition can happen, yes
in this case y is the whole exprssion Vector3.left * speed * Time.deltaTime
you can't break parts of that out and add them to x first
you have to calculate all of it, then add the result to x
i see i see
hence the parentheses
also multiplication happens before addition anyway in bodmas/pemdas
thank you a lot for explaining ill keep those parentheses in mind
yesyes but i dont think c# has bodmas built in does it?
The actual formal list of operator precedence in C# is here btw: https://docs.microsoft.com/en-us/cpp/c-language/precedence-and-order-of-evaluation?view=msvc-170
it does have operator precedence, yes
- comes before +
oh okay thast very handy