I have an issue that I can't solve. There are many questions relating to similar issues and they come down to "change some settings" and "object is travelling too fast". My problem doesn't seem to equate to either of these.
I am running through the basic tutorial for the Breakout Game and have added some of my own tweaks to the code, my issue is that even at low speed my ball overlaps with the bricks, rather than bouncing off them.
I have read various related questions but nothing seems to fix this for me.
Things I've done:
- Set Project Settings >> Physics2d >> Maximum penetration For Overlap = 0.0001
- Set Ball RigidBody Collision detection to "Continuous Dynamic" .
- Set Brick RigidBody Collision Detection to "Continuous"
- Forced a speed limit on the ball of
2515. Which I think is not fast at all, as most the given solutions are talking about situations arising when the offending object is travelling at much higher speeds. - Bricks are standard rigidbody cubes and ball is a standard rigidbody sphere.
- According to this answer I have also set the rigidbody interpolation to "interpolation" for both ball and bricks. But this seems processor expensive and doesn't solve the issue.
- As well as set the Project Settings >> Physics >> Solver Iteration Count to 25. This does seem to have helped somewhat (ball no longer gets "stuck" inside brick wireframes, but still overlaps).
Below are some screenshots of my issue and my code.
The only aspect that I can't fully account for is that I've added an acceleration function so when ball is launched it accelerates over a course of several seconds to it's max speed. It's start speed is 8 (although the force applied is actually 1000 when launched, ignore that figure), and the max speed is set to 25 15. I don't know enough to say if this is somehow "breaking" rigidbody overlap detection but it seems pretty silly if so. See line 63 below:
Ball Source Code:
using UnityEngine;
using System.Collections;
public class Ball : MonoBehaviour {
public float initialVelocity = 500f;
public float accelerationSpeed = 5f;
public float MaximumSpeed = 15f;
private Rigidbody rb;
private bool ballInPlay = false;
void Awake () {
rb = gameObject.GetComponent<Rigidbody>();
}
void Update () {
if (Input.GetButtonDown("Fire1") && ballInPlay == false)
{
transform.parent = null;
rb.isKinematic = false;
rb.AddForce(new Vector3(initialVelocity,initialVelocity,0));
ballInPlay = true;
}
}
void FixedUpdate ()
{
/***
* Gradually increase ball speed
***/
if (ballInPlay == true && rb.velocity.magnitude < MaximumSpeed)
{
speedUp();
Debug.Log("Ball Speed is "+rb.velocity.magnitude);
}
/***
Clamp to maximum speed
***/
if (ballInPlay == true && rb.velocity.magnitude > MaximumSpeed)
{
rb.velocity = rb.velocity.normalized * MaximumSpeed;
}
}
// rb.velocity.magnitude
void speedUp()
{
/***
This is line 63:
***/
rb.AddForce(rb.velocity.normalized * accelerationSpeed,ForceMode.Acceleration);
}
}
Why are the rigidbodies not detecting and preventing overlap?