The question is answered, right answer was accepted
InvokeRepeating not working
Hi guys,
So I'm trying to make the speed of my shuriken increase by 2 after 30 seconds, and then every 30 seconds after that. From what I read in the documentation, this should be how it's done?
private Rigidbody2D rb2d;
public float speed = 10f;
public GameObject DownShuriken;
public static int livess = 2;
void Start ()
{
rb2d = GetComponent<Rigidbody2D> ();
}
void Update ()
{
rb2d.AddForce(Vector2.down*speed);
InvokeRepeating ("IncreaseSpeed", 30, 30f);
}
void SpawnShurikenDown()
{
Vector3 RandomSpawn = new Vector3 (Random.Range (-9.1F, 9.1F), Random.Range (5.04F, 5.04F),1F);
Instantiate (DownShuriken, RandomSpawn, Quaternion.identity);
}
void OnTriggerExit2D(Collider2D other)
{
if(other.gameObject.CompareTag ("BottomBackground"))
{
SpawnShurikenDown ();
Destroy (DownShuriken);
}
}
void IncreaseSpeed ()
{
speed = speed + 2f;
}
}
After 30 seconds, the speed of the shuriken does not change. A Debug.LogError (speed) does nothing inside the function IncreaseSpeed when put beneath the line where I change the speed.
I use the variable name speed in other scripts but nothing else is accessing it in this script.
Try just Invoke to see if that works but I'm certain you can't call invoke or invoke repeating in update anyway because it would call it every frame and cause chaos!
Also one of the floats in the invoke repeating is declared at a float with f but thaw other isn't . That might be causing some craziness there .
Answer by Toon_Werawat · Mar 22, 2016 at 08:20 PM
Maybe update was reset InvokeRepeating?
If so. You can try alternative.
By replace
void Update ()
{
rb2d.AddForce(Vector2.down*speed);
InvokeRepeating ("IncreaseSpeed", 30, 30f);
}
And With this script
public float lastTime;
void Update ()
{
rb2d.AddForce(Vector2.down*speed);
if (Time.time > lastTime)
{
lastTime = Time.time + 30;
IncreaseSpeed();
}
}
This does work, thank you.
But I don't understand.. If InvokeRepeating was being reset by Update(), would I be able to call InvokeRepeating else where? like Start()?
Yes. Another place that not update. (Or Fixed/Late update)
For some reason that doesn't work though?
private Rigidbody2D rb2d;
public float speed;
public GameObject DownShuriken;
public static int livess = 2;
void Start ()
{
rb2d = GetComponent<Rigidbody2D> ();
InvokeRepeating ("IncreaseSpeed", 30, 30f);
}
void Update ()
{
rb2d.AddForce(Vector2.down*speed);
}
void SpawnShurikenDown()
{
Vector3 RandomSpawn = new Vector3 (Random.Range (-9.1F, 9.1F), Random.Range (5.04F, 5.04F),1F);
Instantiate (DownShuriken, RandomSpawn, Quaternion.identity);
}
void OnTriggerExit2D(Collider2D other)
{
if(other.gameObject.CompareTag ("BottomBackground"))
{
SpawnShurikenDown ();
Destroy (DownShuriken);
}
}
void IncreaseSpeed ()
{
speed = speed + 2;
}
}
Hm? $$anonymous$$aybe I should try it on my PC. And see what it does
Did you try to make item Spawn everytime it exit trigger? Try to make sure. You tick "is$$anonymous$$inematic" And at trigger. It have Collider2D with tick "isTrigger" Just asking. I'm testing here. And It not moving at all. ._,
Follow this Question
Related Questions
Adding Speed 0 Answers
accelerate speed 0 Answers
How to include speed trigger creation option in this script. 0 Answers
Getting two Objects to the same direction with different speed 0 Answers
How to make sphere a cricket ball? 1 Answer