Search Unity

movement problems

Discussion in 'Scripting' started by oldcollins, Mar 24, 2015.

  1. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    i have this script

    Code (JavaScript):
    1. var speed : float = 5.0;
    2.  
    3.  
    4. var maxheight : float = 898;
    5. var minheight : float = -898;
    6.  
    7. function Update () {
    8.  
    9.      if( minheight <=-898)
    10.      {
    11.          transform.Translate(Vector3(0,speed,0) * Time.deltaTime);
    12.      }
    13.    
    14.      else
    15.    
    16.      if( maxheight >= 898)
    17.      {
    18.          transform.Translate(Vector3(0,-speed,0) * Time.deltaTime);
    19.      }
    20. }
    and the objects goes up but doesn't go down and im wondering if there is a way to make this work with more then one object and randomize the movement
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    I don't think comparing minheight and maxheight with fixed values is what you want. You should compare the object's actual position to minheight/maxheight :)

    They way you did it, the first condition is always true, so the object will always go up.
     
  3. oldcollins

    oldcollins

    Joined:
    Aug 3, 2012
    Posts:
    111
    what would be the best why to do that as im trying different ways at the min and this is something im looking to get working as soon as