#recoil

1 messages · Page 1 of 1 (latest)

hushed flicker
#

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

warm dove
#

What do you mean by return? I thought returning would mean it would return a value, like an int, string, etc

hushed flicker
#

A function that returns void doesn't return anything

warm dove
#

Wouldn't it stopped working without it?Oh yeah

hushed flicker
hushed flicker
#

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.

warm dove
#

so "if (recoil) return", should go at the beggining of FixedUpdate()?

hushed flicker
#

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

warm dove
#

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

hushed flicker
#

Right.

#

FixedUpdate runs 50 times per second (once every physics update)

#

As long as recoil is set to true, the function will immediately quit

warm dove
#

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

hushed flicker
#

you're welcome (:

warm dove
#

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

hushed flicker
#

that's simple enough --

Vector3 mousePos = Camera.main.ScreenToWorldPos(cursorPosition);
Vector3 forceDir = mousePos - transform.position;
forceDir = forceDir.normalized;
warm dove
#

God damn, you're a genius

hushed flicker
#
  • 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

warm dove
#

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

hushed flicker
#

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

warm dove
#

Ooooh yes yes, I see what you mean

hushed flicker
#

so, normalizing it gives you a vector of length 1

#

you can multiply that by the shoot force to get a force vector

warm dove
#

Because it should be the same force regardless of how far the mouse is to the player

hushed flicker
#

right.

warm dove
hushed flicker
#

When you need it, yes

warm dove
#

I'm getting an error message here

hushed flicker
#

cursorPosition isn't defined

#

you'll need to get that from somewhere