#`moveSpeed Time deltaTime Vector3 left`

1 messages · Page 1 of 1 (latest)

meager lily
#

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?

lament fjord
#

not really relevant here as all the operations are *

#

it just goes left to right

meager lily
#

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
lament fjord
#

show your code

meager lily
#
void Movement()
{
        transform.position += moveSpeed * Time.deltaTime * Vector3.left;
}
lament fjord
#

and the old code?

meager lily
#
void Movement()
{
        transform.position += Vector3.left * moveSpeed * Time.deltaTime;
}
lament fjord
#

ok yeah you'll get the same result

#

are you seeing different results?

meager lily
#

yeah

#

well

lament fjord
#

🤔

#

I kinda doubt that...

meager lily
#

hmm

lament fjord
#

what are you seeing that's different?

meager lily
#

the old code would run as -1,0,0 by the end whereas the new one would end as 0,0,0

lament fjord
#

Is this something you're observing or something you're guessing?

#

wdym by "the end"?

#

The end of what

meager lily
lament fjord
#

your calculations are wrong

#

run the game and test it out, it will behave the same

meager lily
#

hmm okay

#

holf

#

hold*

#

okay

meager lily
#

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?

lament fjord
#

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)```
meager lily
#

hm alright but what happened to the addition?

lament fjord
#

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

meager lily
#

so is it when i use += that it first does the other things before adding it?

lament fjord
#

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

meager lily
#

i see i see

lament fjord
#

hence the parentheses

#

also multiplication happens before addition anyway in bodmas/pemdas

meager lily
#

thank you a lot for explaining ill keep those parentheses in mind

meager lily
lament fjord
#

it does have operator precedence, yes

#
  • comes before +
meager lily
#

oh okay thast very handy