Search Unity

Object moving to one point and back

Discussion in 'Scripting' started by SiLen7, Feb 3, 2013.

  1. SiLen7

    SiLen7

    Joined:
    Feb 3, 2013
    Posts:
    1
    Hello,

    How can I make that cube will move forward and back automaticlly?
    Sorry for my bad english

    bye
     
  2. FlyingAce

    FlyingAce

    Joined:
    Jan 12, 2013
    Posts:
    58
    var startPos : Vector3;
    var distance : float;

    function Awake ()
    {
    startPos = transform.position;
    }
    function Update ()
    {
    transform.position = startPos+Vector3(Mathf.Sin((1/distance)*Time.time),0,0);
    }

    Not completely sure what you want, but here's something to play around with. Make the direction you want to go one of the values on the Vector3
    Hope this helps!
     
    Last edited: Feb 3, 2013