Object moves to -infinite... help!

Hi There:

I’m doing a platformer game, and i’ve planned a pick up that lets you fly, so when you pick it up, you can move up and down when you get input from the vertical axis…

this is my first game, that i’m coding, so i’m really bad at this, and i can’t get this function to work properly.

The set up is the following:

The character has a bool that checks to check if it can fly, that is turned to true when you pick up the crate, that works… the problem is as soon as i pick up the crate, the player is translated to -infinite in the y position and the game crashes…

This is my example code:

public bool kiteOn = false;  // checks to see if it can fly
public float kiteTime = 4.0f;  // amount of time it can fly
public float kiteSpeed = 1.0f;  // movement speed while flying

void Update()  
...
if (kiteOn)  // on update it checks it, and calls the function
{ninjaKite ();}
}


public void ninjaKite()   // the function to make it "fly"
	{
		float timer = 0.0f;   
		while ( timer < kiteTime) 
		{
		timer += Time.deltaTime;
		inAir = true;
		if (this.transform.position.y < 1.0f | this.transform.position.y > -1.0f)
			{
		float tmpY = this.transform.position.y + kiteSpeed * Input.GetAxis("Vertical");
		this.transform.Translate (0.0f, tmpY, 0.0f);
	        }

	}
	kiteOn = false;

}

Got it. Your while loop is executing entirely in a single frame.

Couple of solutions

  1. Make NinjaKite a coroutine
  2. Move the timer functionality out into update
  3. Change the structure so that the timer check is done in an if

You’ve got a bunch of other problems to consider as you fix this too

  • NinjaKite is called every frame. You reset your timer at the start of NinjaKite
  • Consider using Mathf.Clamp on tmpY. Its more readable
  • Using common coding conventions around indentation and capitalisation will help you a lot in the long run

Haha, easy one, we’ll solve this in a fraction of time :3
You used to define a:

float timer = 0;

In the ninjaKite() method, which in turn was called very often, to up the timer, which is logical, however, making a new timer every update will keep it at roundabout 0.

So, now here’s some new code.
Note that we’ve only fixed the timer / crash issue, you might still want to edit the kiteSpeed value.

Also, I’m not sure what inAir does, but that’s up to you :wink:

    public bool kiteOn = false;  // checks to see if it can fly
    public float kiteSpeed = 1.0f;  // movement speed while flying
    public float kiteTimer_Max = 4.0f;  // amount of time it can fly
    private float kiteTimer_Cur; // Define the timer_Cur up here
 
    void Update() {
        //...
        if (kiteOn)  // on update it checks it, and calls the function
        {
            ninjaKite ();
        }
    }
 
 
    public void ninjaKite()   // the function to make it "fly"
    {
        // Increment timer
        kiteTimer_Cur += Time.deltaTime;
        inAir = true; // ?

        // Fly upwards when -1 < y < 1
        if (this.transform.position.y < 1.0f | this.transform.position.y > -1.0f)
        {
            // Added time.deltaTime to smooth out the translation just a bit (in case you have lower fps => same result, because of Time.deltaTime) 
            // You might need a higher speed value though, but that's a number change.

            // Calculate a new Y and fly
            float tmpY = this.transform.position.y + kiteSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
            this.transform.Translate(0.0f, tmpY, 0.0f);
        }

        if (kiteTimer_Cur >= kiteTimer_Max)
        {
            // Turn off the flying and reset the timer
            kiteTimer_Cur = 0;
            kiteOn = false;
        }
    }

I hope that helps, if you need anything else or have other questions, please ask another one or comment down below if it’s related.

Otherwise, please accept this answer and have a nice day! :slight_smile: