#recoil
1 messages · Page 1 of 1 (latest)
making a thread
This is the function that we're talking about.
if (recoil) return; will return from FixedUpdate if recoil is true.
But it's at the end of FixedUpdate, so it doesn't really accomplish anything
returning from a function means that nothing else will happen in that function -- you're done
What do you mean by return? I thought returning would mean it would return a value, like an int, string, etc
So why is it there?
A function that returns void doesn't return anything
Wouldn't it stopped working without it?Oh yeah
Because you put it on the wrong line 😛
Oooooh
Returning before the end of a function is sometimes called "early return"
It's useful for writing code that looks like this:
- If X and Y and Z are all true, do W
if (!X)
return;
if (!Y)
return;
if (!Z)
return;
In this case, FixedUpdate only had one line of code in it
so it's less obviously useful to do this
void FixedUpdate() {
if (!recoil)
rb.velocity = new Vector2(Horizontal * speed, rb.velocity.y);
}
void FixedUpdate() {
if (recoil)
return;
rb.velocity = new Vector2(Horizontal * speed, rb.velocity.y);
}
These both achieve the same thing
The first one only sets the velocity if recoil is false
The second one returns early if recoil is true. If it doesn't, it sets the velocity.
so "if (recoil) return", should go at the beggining of FixedUpdate()?
Right
early return can help prevent excessively nested code
if (X) {
if (Y) {
if (Z) {
...
}
}
}
again, it's not that obviously useful here
since you've only got one line of code
I would use it in a situation like
void FixedUpdate() {
PlayerMovement();
// more code
}
void PlayerMovement() {
if (!allowedToMove)
return;
// lots and lots of code
}
if you aren't allowed to move, PlayerMovement immediately bails out
Correct me if I'm wrong, but I'm initially setting the recoil bool to false. Then when left mouse click, it starts the coroutine which sets the recoil to true. During the next 0.5 seconds, the new velocity is set and FixedUpdate() will see that recoil is true and stop(?) then the recoil is set back to false
Right.
FixedUpdate runs 50 times per second (once every physics update)
As long as recoil is set to true, the function will immediately quit
Okay, I think I've finally understood it
OMG it's actually working now!!!!
Thank you so much!!!
You've been so helpful!!!!
Thank you, you've taught me a lot
you're welcome (:
now I have to figure out how to apply the force is the opposite direction of where the mouse is lol
right now it's just pushing the player to the side
But that's a mission for another day lol
that's simple enough --
Vector3 mousePos = Camera.main.ScreenToWorldPos(cursorPosition);
Vector3 forceDir = mousePos - transform.position;
forceDir = forceDir.normalized;
God damn, you're a genius
- get the world position of the mouse cursor
- subtract the player position from that to get a vector from the player to the cursor
- normalize it so that its length is 1
oops, that's backwards
you want to point towards the player, right
transform.position - mousePos
A - B is a vector that points from B to A
I made a script earlier to make the players gun follow the mouse so I think I kiiinnddaaaa understand this part a little bit lol
What is .normalized?
Oh wait, so normalizing is essentially rounding to the nearest whole number?
Or is it like setting scale? Like in Blender, for example
A normalized vector has a length of 1
When you do transform.position - mousePos, you get a vector whose length is the distance between those two points
But you're going to use this to calculate a force for the player
You don't want the length of the vector to vary
you just want the direction
Ooooh yes yes, I see what you mean
so, normalizing it gives you a vector of length 1
you can multiply that by the shoot force to get a force vector
Because it should be the same force regardless of how far the mouse is to the player
right.
This would go into Update(), right?
When you need it, yes
I'm getting an error message here
cursorPosition isn't defined
you'll need to get that from somewhere
if you're using the old input system, I think that's just https://docs.unity3d.com/ScriptReference/Input-mousePosition.html