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): public class zigzag : MonoBehaviour { Vector3 velocity; Vector3 gravity; void Start () { velocity = new Vector3 (3, 3, 0); gravity = new Vector3 (0, -1, 0); } // Update is called once per frame void Update () { transform.position += velocity * Time.deltaTime; velocity += gravity * Time.deltaTime; if (velocity.y <= -3) { gravity.y = 1; } if (velocity.y <= 0) { gravity.y = -1; } } } Argentina vs Chile WOOT! WOOT!
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): if (velocity.y < -3) { gravity.y = 1; } if (velocity.y > -3) { gravity.y = -1; or simpler Code (CSharp): 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.
The script did not work regardless of your answers but there was a flaw in the algorithms pretty self explanatory. the fix: Code (CSharp): public class zigzag : MonoBehaviour { Vector3 velocity; Vector3 gravity; void Start () { velocity = new Vector3 (3, 3, 0); gravity = new Vector3 (0, -1, 0); } // Update is called once per frame void Update () { transform.position += velocity * Time.deltaTime; velocity += gravity * Time.deltaTime; if (velocity.y <= -3) { velocity.y = 3; } } }