Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to AddForce independent of rotation?

Discussion in 'Scripting' started by djarolol, Oct 21, 2022.

  1. djarolol

    djarolol

    Joined:
    Sep 10, 2018
    Posts:
    2
    Hi! I have a small problem.
    I am adding force to a rectangle like this:
    Code (CSharp):
    1. Rigidbody2D m_Rigidbody;
    2. ...
    3. m_Rigidbody.AddForce(transform.right * m_Thrust);
    Only problem is that "transform.right" seems to be relative to the rotation of the rectangle.
    So if the rectangle is rotated 180 degrees, then "transform.right" actually points to the left.

    How can I add force in a way that it is ALWAYS pointing to the objective right, regardless of how the rigidbody is rotated?
     
  2. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
  3. djarolol

    djarolol

    Joined:
    Sep 10, 2018
    Posts:
    2
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    It's 2D, don't use Vector3, use Vector2.

    Rigidbody2D.AddForce

    Using Vector3 and letting Unity implicitly convert it to Vector2 for you without thinking will lead to subtle bugs, especially when you're doing things such as 2D distance calculations and you're implicitly including Z.
     
    orionsyndrome and Kurt-Dekker like this.