❔ Trying to get better at writting code. any advice?
Any changes you think can be done in the code above? (do note speed variable is a method argument. the method is called either by "GameManager" that manages player input, or by an AI)
49 Replies
Lost of semi duplicate instructions that have the same idea with different values. You can condense it using variables and methods
movement += speed.y * Time.deltaTime * tempSpeed * Vector3.back
This is a good start, if it's 0 the multiplication will resolve to 0 anyway
Though it could be optimized by caching the result of Time.deldaTime * tempSpeed
to a variable
moving = movement > 0
logical operators already return bool so it's simply assigning it directly to the variable
Right, you can't directly compare a Vector3 to an int, so then it should be moving = movement.x > 0 || movement.y > 0
or compare to that though it will have a wrong result if z > 0I wouldn't remove canSprint since I will add a sprinting mechanism
and if I make it all depend on sprinting, some other code may malfunction.
there isn't any abs
this is my new code. any changes now?
Seems good except for the two assignements of moving
You could also set that to
movement != Vector3.Zero
and put moving in the ifwdym
also:
from what I've learned from you guys:
1. try to use as few variables as possible strategically
2. try to make as few branches as possible, math is more optimized than that, and 0 multiplication is always 0 (logically)
Conditions are themselves bool values
yeah but StepClimb and transform.translate should be called only when the player moves
and the player moves when moving would be set to true
and moving would be set to true when movement isn't zero
and there's no abs in vector3, but there's Vector3.zero
which is (0,0,0)
So do the things when moving, which is directly defined from the variable, which is itself defined from a condition
Unless the moving variable is used elsewhere to avoid checking the vector3 too many times, you can get rid of it entirely
the moving variable would be used for animation and sounds and stuff
If you do need the variable, it's always set to true when not zero and false when zero, so you can condense that logic as a single bool assignement
moving = movement != Vector3.Zero
also that is in a move method
so it's
and Move is called either by an AI when he wants, or when player inputs
Since the vector was already compared once and the result stored in moving, you can use that as the condition in the if
if (moving) {}
bools and conditions are interchangable, more like they're both boolthe speed variable also shouldn't really be a speed, it should rather be -1 to +1 (as AD and WS, but also controller support)
so I can do if (movement != Vector3.zero) instead of that bool?
oh
The opposite
Set moving to the condition and check moving
moving = movement.magnitude == 0 ? false : true;
is magnitude good
idk what it does but I've seen it's smth like x*x + y*y..
If you have controller support, make sure the values can be decimal to allow for analog movement
the input supports controllers, but the game won't have other devices support other than pc for a long time anyway
Most pc games also have controller support
but what do I do if the speed is more or less than -1 and +1?
does magnitude work?
It wouldn't. It would just be a decimal number in the range -1, 1
where -1 or 1 is the max speed in the matching direction
no like
moving = movement.magnitude == 0 ? false : true;
Is it a top down where moving up isn't jumping?
it's a 3d
first person
Then magnitude would be > 0 whenever airborne
but I did not implement anything about jumping rn, and idk if I should add it
move method will not be called when gravity
it will be called either by AI or by player input
Magnitude is if you drew a box with the dimensions of the vector and made a 3d arrow going from one corner to the opposite corner.
The magnitude is the length of the line/arrow
And if I recall, unity uses Y for the vertical axis, so moving and strafing should use x and z
so if speed is 0,0,0, then the magnitude would be 0, right?
yes
so I can use magnitude
or Vector3.zero
The vector is obtained from the input right?
is this better?
Where does direction come from?
input or AI
basically this is input:
also yes I know I can do player != null && controllablePlayer but I might have added something else (I think I'll just wrap the two if statements for now)
Well if you only care about it moving in any direction, you can use magnitude
Though depending on how it's implemented in Vector3, that may be expensive to do
I'd move it in x and z, there's no jumping mechanisms yet buy in case I add, I plan checking if direction.y is > 0
It's suggesting using square magnitude if it's only for comparison so safe to say it gets calculated on the fly
Then it's faster to just use Vector3.Zero
ah lol didn't see that
notice
What I've been suggesting earlier
Then
if (moving) {}
so is this neat?
ok
and should I revert that if I add non-player code in FixedUpdate?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
why var?
does it do anything apart from shortening Vector3?
so like this
so it just shortens the code
ok
then next time I create variables, I'll always use var
local*
next time I make local variables, I will use var
wdym subjective
I never listen to my teacher to know what that means
?
var
is good, it's redundant to declare inferred types. intellisense can give you the type info.I do get js flashbacks and I really hate js for not having types
js has types, it just does a lot of coercion.
it's not statically typed, though.
I prefer ts
ts IS static. it's much better.
anyway back to my code
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.