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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rails movement

Discussion in '2D' started by EdgarCarrera, Dec 6, 2016.

  1. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Hi guys i need some help i explain on pictures.

    i need move objects on platform by direction example on this picture.

    upload_2016-12-6_12-9-2.png

    So it's simle the red box is a cube and gray rectanlge is the platform and i want platform move cube to arrow directions. i'm trying witih rigidbody and transform but got some errors. because if the box have rotation 90 - 90 -180 180 they move wrong direction. can u help me a clean and simple code?
     
  2. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Done: :D

    Code (CSharp):
    1.         if (_start.transform.position.x < _end.transform.position.x)
    2.         {
    3.             collision.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(1f* Vector2.right.x, 0);
    4.         }
    5.         else
    6.         {
    7.             collision.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(1f * -Vector2.right.x, 0);
    8.         }
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Is this like conveyor belt style movement? There's a Surface Effector 2D component that you can put on your platform to move rigidbodies along it automatically.