Search Unity

Question Addforce relative to transform

Discussion in '2D' started by Bugges, Apr 17, 2019.

  1. Bugges

    Bugges

    Joined:
    Mar 9, 2019
    Posts:
    9
    private void update()
    {
    gameObject.transform.position.x + speed * Time.deltaTime;
    }

    speed is a float = 15
     
  2. Bugges

    Bugges

    Joined:
    Mar 9, 2019
    Posts:
    9
    I want to do something like addforce. But it has to be in the X axis of the object when it rotates, and when i use addforce it just use the X axis of the world and starts going right even if it was rotated.
     
  3. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    You could create a vector3 and have the y and z values set to zero then just use transform.position += yourVector3
     
  4. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Code (CSharp):
    1. transform.position = new Vector3(transform.position.x + speed *Time.deltaTime, transform.position.y, transform.position.z);
    If you want to account for rotation you need to use transform.right
     
  5. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    or (rigidbody version)
    Code (CSharp):
    1. rigidbody2D.AddForce(Vector2.right * speed);
    Code (CSharp):
    1. rigidbody2D.AddRelativeForce(Vector3.right * speed);