0
\$\begingroup\$

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 25 15. 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. Wireframe of collision

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?

\$\endgroup\$
  • \$\begingroup\$ Please paste code as text, not as images. Images make it much harder to copy-and-paste your code to try to reproduce the problem, or to annotate it in an answer. Be sure to use proper code formatting (the "{}" button in the post editing tool) \$\endgroup\$ – DMGregory Nov 17 '16 at 17:56
  • \$\begingroup\$ Sorry, thats a good point. Question updated, \$\endgroup\$ – Martin Nov 17 '16 at 18:01
1
\$\begingroup\$

First - you don't need SpeedUp(); - your rigidbody on the ball has no angular drag and no drag, they are 0. Also you could change Friction to 0 in physics material. Your ball won't lose speed.

Second - check if your bricks have box collider on them and it's not trigger. Also you can remove rigidbody from your bricks if they don't move using Physics. It will give you better performance. Only 1 body that is colliding can have rigidbody to detect collisions - no need for other bodies to have rigidbody.

I would have left this answer in comments if I had more than 50 points - I'm just not allowed to do so now.

\$\endgroup\$
  • \$\begingroup\$ Hi Candid Moon, thanks for your feedback, I will make the adjustments and see how the objects behave and get back to you :) \$\endgroup\$ – Martin Nov 21 '16 at 12:19
  • \$\begingroup\$ re: Speed up, I know the ball isn't loosing speed but at the start of play I have ball at speed 3, and want the ball to speed up during play to speed 8, for example. \$\endgroup\$ – Martin Nov 22 '16 at 20:37
  • \$\begingroup\$ SOLUTION: Removing RigidBody from the bricks solved my issue, and means the ball and bricks now do not overlap. Thanks for the pointers :) \$\endgroup\$ – Martin Nov 22 '16 at 20:38
  • \$\begingroup\$ Glad I could help. Also if you want to speed up to 8, you can make use of coroutines which won't check if the speed is 8 every frame, but will do their work and end. \$\endgroup\$ – Candid Moon _Max_ Nov 23 '16 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.