Search Unity

Is this possible in any way?

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

  1. fbj070106

    fbj070106

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

    speed is a float = 15
     
  2. fbj070106

    fbj070106

    Joined:
    Mar 9, 2019
    Posts:
    3
    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:
    308
    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:
    308
    or (rigidbody version)
    Code (CSharp):
    1. rigidbody2D.AddForce(Vector2.right * speed);
    Code (CSharp):
    1. rigidbody2D.AddRelativeForce(Vector3.right * speed);
     
unityunity