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 ;)
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! :)
↧