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. Dismiss Notice

script not working properly

Discussion in 'Scripting' started by Jorjor210, May 12, 2015.

  1. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I'm trying to make a script that when it a object hit the floor it will go up. but it's currently not working. what are my errors?

    Code (CSharp):
    1.     public Vector3 currentDirection;
    2.     void Update() {
    3.         //Move object down
    4.          currentDirection = Vector3.down;
    5.         transform.Translate(currentDirection * Time.deltaTime);
    6.     }
    7.     void OnCollisionEnter (Collision col)
    8.     {
    9.         if(col.gameObject.name == "Floor")
    10.         {
    11.             currentDirection = Vector3.up;
    12.             transform.Translate(currentDirection * Time.deltaTime);
    13.         }
    14.     }
    15. }
    16.  
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    /sigh... try to give more information rather than here's my script, whats wrong with it.

    what behavior are you seeing?

    by the look of it, it will always move down, never up.

    dont set currentDirection = vector3.down in Update. It just overrides the change to up.

    put it in start...

    ie

    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.  currentDirection = Vector3.down;
    5. }
    6.  
    7. void Update()
    8. {
    9.   transform.Translate(currentDirection *Time.deltaTime);
    10. }
    11.  
    12. //insert the collsion code here
    13.  
    14.  
     
  3. Jorjor210

    Jorjor210

    Joined:
    May 3, 2015
    Posts:
    91
    I appreciate the the help. sorry to inconvenience you with lack of info? :/ you seemed to have managed regardless, so thanks.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    no inconvenience... more of a 'in the future' thing. ;)