- Home /
Prevent a fast moving ball from passing through a wall
I need help with preventing a fast moving ball from passing through a wall. For a testing environment I have a basic sphere with a sphere collider radius .5, and a rigid body that is set to continuous collision detection. The wall is 1 unit thick with a box collider, and the paddle is .8 units thick and also has a box collider. The distance from the paddle to the wall is 57. Every time the ball hits the paddle its velocity is increased by 1. With out fail every time the velocity of the ball reaches 54 it will pass through the wall. I have tried all the built in collision detection methods, but regardless of which is selected it will pass through the wall at velocity total of 54. Here is an example of code used to increase the velocity of the ball, this code is attached to the paddle; I have cut out all the math for dealing with the angles of the bounce, and would like to just focus on the speed issue.
using System;
using UnityEngine;
using System.Collections;
public class PaddleScript : MonoBehaviour
{
public float ballSpeed = -20f;
public float velX = 0;
public float velY = -20f;
void OnCollisionEnter(Collision collision)
{
if(velY>0)
{
velY = ballSpeed - Math.Abs(velX);
}
else
{
velY = (ballSpeed - Math.Abs(velyX));
}
collision.rigidbody.velocity = new vector3(velX,velY,0);
ballSpeed += 1;
}
}
Answer by demize2010 · May 21, 2011 at 07:09 PM
Hi mate,
CCD will not help you if your object is moving very fast. We had a simillar problem with an object that was moved by a player drag action.
The issue is basically that your object moves past the collision point between frames, so even CCD will no save you, but it will munch cycles like no tomorrow.
This script helped us no ends:
http://www.unifycommunity.com/wiki/index.php?title=DontGoThroughThings
That is a great script, it did allow me to speed the ball up to 68 with the skin width set to 1. However I need to get the ball up to a speed of about 90 before capping it out, any other ideas?
The issue is basically that your object moves past the collision point between frames, so even CCD will >no save you, but it will munch cycles like no tomorrow.
This statement isn't true. The whole point of CCD is to stop objects from doing that.
I understand that is the purpose of CCD is to prevent the object from moving though other objects, however it does not do its job past as specific speed; in my case that speed is 54 without the DontGoThroughThings script attached.
Yup CCD improves things but it is still dependent on your frames. You could try CCD and the script at the same time but I'm not 100% confident... have you tried alternate methods of producing the illusion of speed?
Answer by duyanh2812 · Mar 18, 2014 at 06:51 AM
it's also my trouble and i found simple solution when i increase scale object, because your object so small than wall or other object then it's passing through.