Stack overflow caused by "while" loops
I'm very new to coding, so forgive me if I'm missing something obvious here.
I'm trying to make a system where if the 2D player suddenly changes direction with a certain amount of velocity, it hops them back a bit in the opposite direction, cancels out their velocity, and increases drag for two seconds.
However, every timed iteration I've done trying to get the drag to increase has resulted in instant crashed the moment the function is called into action.
Is there some infinite loop or contradiction I'm missing here? Maybe I have artist's eye and it's right in front of me.
Code (please don't laugh):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Vector3 velocity = new Vector3(0f/*x*/, 0f/*y*/, 0f/*z*/);
public Vector2 movement;
public Vector2 position;
public int time = 0;
public int slowtime = 0;
public int slowtimer = 2;
public float bumbdecrease = 0f;
public float bumbdash = 1.0f;
void Start ()
{
StartCoroutine( Example() );
}
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}
IEnumerator Example ()
{
yield return new WaitForSecondsRealtime(1);
time += 1;
}
void FixedUpdate ()
{
velocity = rb.velocity;
position = rb.position;
rb.AddForce(movement.normalized);
if (velocity.x > 6 | velocity.x < -6 | velocity.y > 6 | velocity.y < -6)
{
if (velocity.x > 6 & movement.x < 0)
{
rb.inertia = 0;
velocity.x = -(velocity.x * bumbdecrease);
rb.velocity = velocity;
position.x = position.x - bumbdash;
rb.position = position;
slowtime = (time + slowtimer);
do
{
rb.drag = 12;
} while (slowtime > time);
rb.drag = 6;
}
else if (velocity.x < -6 & movement.x > 0)
{
rb.inertia = 0;
velocity.x = Math.Abs(velocity.x * bumbdecrease);
rb.velocity = velocity;
position.x = position.x + bumbdash;
rb.position = position;
slowtime = (time + slowtimer);
do
{
rb.drag = 12;
} while (slowtime > time);
rb.drag = 6;
}
if (velocity.y > 3 & movement.y < 0)
{
rb.inertia = 0;
velocity.y = -(velocity.y * bumbdecrease);
rb.velocity = velocity;
position.y = position.y - bumbdash;
rb.position = position;
slowtime = (time + slowtimer);
do
{
rb.drag = 12;
} while (slowtime > time);
rb.drag = 6;
}
else if (velocity.y < -3 & movement.y > 0)
{
rb.inertia = 0;
velocity.y = Math.Abs(velocity.y * bumbdecrease);
rb.velocity = velocity;
position.y = position.y + bumbdash;
rb.position = position;
slowtime = (time + slowtimer);
do
{
rb.drag = 12;
} while (slowtime > time);
rb.drag = 6;
}
}
}
}
These StackOverflowException
s are caused by all these while
loops. Stop using them until you familiarize yourself with what they do exactly. Remove them for now and these exceptions will be gone.
How would I accomplish the same thing without the while loops? Is my time iteration incorrect?
Your answer
Follow this Question
Related Questions
Why does this script make unity crash? 1 Answer
Android app crashing on Camera.set_texture 0 Answers
Unity + android 4.0.3 black screen issue 1 Answer
Unity bug reporter not working , I'm not able to post any bug reports Can anybody help? 0 Answers
Unity 5.6.2f1 Crashes on loading a project [FIXED] 3 Answers