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

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