Search Unity

Zig Zag script

Discussion in 'Scripting' started by Wasiim, Jul 4, 2015.

  1. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    So i am trying to make a zig zag script it's a simple script but my problem is on the descent of the zigzag there is a infinite descent and the cube never goes back up. Easy script to understand and i would appreciate some solutions.

    NOTE: Don't worry i know what i'm doing so don't question my decision on naming my variable gravity, i know you can implement gravity in unity easily but i am trying a different approach.

    the script:
    Code (CSharp):
    1. public class zigzag : MonoBehaviour {
    2.  
    3.     Vector3 velocity;
    4.     Vector3 gravity;
    5.     void Start () {
    6.         velocity = new Vector3 (3, 3, 0);
    7.         gravity = new Vector3 (0, -1, 0);
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update () {
    12.         transform.position += velocity * Time.deltaTime;
    13.         velocity += gravity * Time.deltaTime;
    14.  
    15.         if (velocity.y <= -3) {
    16.             gravity.y = 1;
    17.         }
    18.         if (velocity.y <= 0) {
    19.             gravity.y = -1;
    20.         }
    21.     }
    22. }
    Argentina vs Chile WOOT! WOOT!
     
    Last edited: Jul 4, 2015
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I think both if statements will always be true, therefore you always end up with whatever the last if statement gives.

    Try this
    Code (CSharp):
    1.         if (velocity.y < -3) {
    2.             gravity.y = 1;
    3.         }
    4.         if (velocity.y > -3) {
    5.             gravity.y = -1;
    6.  
    or simpler
    Code (CSharp):
    1. gravity.y = (velocity.y < -3) ? 1 : -1;
    In the future, remember to start putting debug.logs in your code to test to see what is happening. If statements are a great place for debug.logs.
    If you put a debug.log in both your if statements, you would have seen it debug.logging both of them all the time which would have let you know something was wrong.
     
    Last edited: Jul 4, 2015
    Wasiim likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Simply change the direction of the sign on line 18. <= should be >=
     
  4. Wasiim

    Wasiim

    Joined:
    May 2, 2015
    Posts:
    228
    The script did not work regardless of your answers but there was a flaw in the algorithms pretty self explanatory.

    the fix:
    Code (CSharp):
    1. public class zigzag : MonoBehaviour {
    2.  
    3.     Vector3 velocity;
    4.     Vector3 gravity;
    5.     void Start () {
    6.         velocity = new Vector3 (3, 3, 0);
    7.         gravity = new Vector3 (0, -1, 0);
    8.     }
    9.  
    10.     // Update is called once per frame
    11.     void Update () {
    12.         transform.position += velocity * Time.deltaTime;
    13.         velocity += gravity * Time.deltaTime;
    14.  
    15.         if (velocity.y <= -3) {
    16.             velocity.y = 3;
    17.         }
    18.     }
    19. }