- Home /
How to fix player bouncing multiple times
I have a bounce pad in my game and it bounces the player. it's in a OnCollisonEnter2D function and it looks like this (this is a part of the script)
if (transform.tag == "Bouncer")
{
Rigidbody2D rb = collision.transform.GetComponent<Rigidbody2D>();
if (rb != null)
{
rb.AddRelativeForce(new Vector2(0f, BounceForce), ForceMode2D.Impulse);
}
}
I also have a slowing down time feature in my game that slows down time when the player presses Q. But if I do this while going onto the bounce pad, It bounces the player multiple times instead of once. Please help.
Answer by locolitovega · Apr 16, 2020 at 07:48 PM
Alright, figured it out. I used DateTime to ignore the second bounce, here's how i did it but it may vary from games because this is for a slow motion game like karlson or superhot. Timebounce is my DateTime variable. Make sure to set yours to DateTime.Now when you create the variable
if (TimeBounce.AddSeconds(.2 / Time.timeScale) <= DateTime.Now)
{
if (rb != null)
{
Debug.Log("Bounce");
rb.AddRelativeForce(new Vector2(0f, BounceForce), ForceMode2D.Impulse);
TimeBounce = DateTime.Now;
}
}
Answer by locolitovega · Apr 16, 2020 at 07:00 PM
Sort of figured it out, the OnCollisionEnter is getting called twice for some reason but i still don't know how to fix the problem.
Answer by locolitovega · Apr 16, 2020 at 07:48 PM
Alright, figured it out. I used DateTime to ignore the second bounce, here's how i did it but it may vary from games because this is for a slow motion game like karlson or superhot. Timebounce is my DateTime variable. Make sure to set yours to DateTime.Now when you create the variable
if (TimeBounce.AddSeconds(.2 / Time.timeScale) <= DateTime.Now)
{
if (rb != null)
{
Debug.Log("Bounce");
rb.AddRelativeForce(new Vector2(0f, BounceForce), ForceMode2D.Impulse);
TimeBounce = DateTime.Now;
}
}
Your answer
Follow this Question
Related Questions
Change gravity on button 1 Answer
move, destroy self, instantiate again, repeat. 2 Answers
Player Tips - Have triggers act independently 1 Answer
Staring a 2D game 2 Answers
2D platformer- getting errors I don't understand (c#) 1 Answer