How to set a minimum and maximum bounce on a ball
I had a Gameobject (ball) with a physics2d material that had a bounciness of 1f. Pressing Space changes the velocity so it goes down instantly and faster than normal (think pushing a basketball down with your hand). I also set a maximum height in the script and when this is reached the velocity on the y axis is set to zero so it falls and immediately bounces back.
That worked fine, problems came in when I wanted the ball to lose momentum with each bounce normally through gravity BUT have a mimimum bounce height so it is forever bouncing at at least the minimum height and pressing space will cause it to bounce higher until max and if I left it it'll lose energy and eventually get back to the minimum bounce infinitely.
I tried having two materials. one with a bounciness of 1f and the other 0.8f and im currently trying to detect when the ball is bouncing just high enough to get to minimum bounce and if this is the case switch from the 0.8f material (normalMat in the script) to the 1f material (fullBounceMat in the script) so it stays bouncing . if I manage to press space and the ball has enough energy to go past the mimimum height I want to swap back to the 0.8f material.
Im failing terribly and im sure there has to be an easier way
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallBounce : MonoBehaviour
{
//Components
Rigidbody2D rb;
Animator anim;
Collider2D myCollider;
public PhysicsMaterial2D normalBounceMat, fullBounceMat;
//Ball Speeds
public float moveDownSpeed;
//Will be added to the move down speed when player presses Space
public float moveDownSpeedIncrease = 10f;
//Storing Inspector Speed Values for Resetting Later
private float defaultMoveDownSpeed;
//Ball Heights
public float minHeight, maxHeight, defaultMaxHeight, currentBallHeight, previousBallHeight;
private bool falling = true;
private bool canStopYvelocity = true;
//visual cue to see if detecting falling correctly
private bool showDropColourIndicator = false;
void Start()
{
//Caching Components
rb = GetComponent<Rigidbody2D>();
rb.sharedMaterial = fullBounceMat;
anim = GetComponent<Animator>();
myCollider = GetComponent<Collider2D>();
//Caching Defaults
defaultMoveDownSpeed = moveDownSpeed;
defaultMaxHeight = maxHeight;
currentBallHeight = transform.position.y;
previousBallHeight = minHeight;
StartCoroutine(CheckMinHeight());
}
void Update()
{
PushBallDown();
CheckMaxHeight();
}
//Meant to immediately "push" the ball down faster after pressing space
void PushBallDown()
{
if (Input.GetKeyDown(KeyCode.Space))
{
moveDownSpeed += moveDownSpeedIncrease;
rb.velocity = new Vector2(rb.velocity.x, - moveDownSpeed);
}
}
void CheckMaxHeight()
{
if (transform.position.y >= maxHeight)
{
if (canStopYvelocity)
{
canStopYvelocity = false;
//Stoping the ball from going up further than the max height
rb.velocity = new Vector2(rb.velocity.x, 0f);
}
}
}
IEnumerator CheckMinHeight()
{
while (true)
{
yield return new WaitForSeconds(0.1f);
currentBallHeight = transform.position.y;
if (currentBallHeight >= previousBallHeight)
{
previousBallHeight = currentBallHeight;
falling = false;
if (showDropColourIndicator)
{
anim.SetBool("falling", false);
}
}
else
{
ChooseMaterial();
falling = true;
if (showDropColourIndicator)
{
anim.SetBool("falling", true);
}
}
}
}
void ChooseMaterial()
{
if (previousBallHeight <= minHeight)
{
myCollider.sharedMaterial = normalBounceMat;
}
else if (previousBallHeight > minHeight)
{
myCollider.sharedMaterial= fullBounceMat;
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
canStopYvelocity = true;
moveDownSpeed = defaultMoveDownSpeed;
previousBallHeight = transform.position.y;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612201141im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problems with raycasting 2D. Probelmas com raycasting 2D. 0 Answers
How to let player hit a boxcollider just once?(2D) 1 Answer
Top-down 2D - crawling script 0 Answers
Bubble Bubble with unity 0 Answers