Search Unity

how can io add velocity relative to my player rotation?

Discussion in 'Physics' started by FireJojoBoy, May 20, 2020.

  1. FireJojoBoy

    FireJojoBoy

    Joined:
    Oct 30, 2019
    Posts:
    65
    when the Player presses a key, this code is called:
    rb.velocity += new Vector2(0, probs.speed);

    this makes the player move up, no matter if the player is rotated to the front.
    how can i make the player move to his front?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Like this:
    Code (CSharp):
    1. rb.AddRelativeForce(Vector2.up * probs.speed, ForceMode2D.Impulse);
    Change Vector2.up to .down, .left, .right depending on where zero rotation starts.

    Note that if you do this per-frame and don't scale the change by time then you'll get a frame-rate or fixed-update dependant acceleration which is something you probably don't want. Scaling your speed by the elapsed time (Time.deltaTime or Time.fixedDeltaTime) will solve that.
     
    FireJojoBoy likes this.