I’m working on a 2D volleyball game and the issue I’m having relates to the ball part of the game.
This is the code I’ve used so far.
CircleCollider2D cc2d;
[SerializeField] Rigidbody2D rb2d;
[SerializeField] Vector2 ballForce1;
[SerializeField] Vector2 ballForce2;
// Start is called before the first frame update
void Start()
{
float r1 = Random.Range(1, 2);
float r2 = Random.Range(1, 2);
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (transform.position.x > 0)
{
rb2d.AddForce(ballForce2);
}
else if (transform.position.x < 0)
{
rb2d.AddForce(ballForce1);
}
}
I’ve also set the parameters for the Vector2 variables and the bounciness of the Physics Material 2D that the ball is connected on to 1.
However, it still doesn’t work as expected. The ball either bounces too quickly, bounces in the direction it came instead of the opposite direction or bounces onto the floor.
How can I fix this?
Thanks.