- Home /
Ball goes through walls ????
I am new to unity. I am creating a game with ball. I have created a maze with walls(CUBE) surrounded to it. When i move the ball it goes through the walls. I need to fix this. I have used thin box colliders to prevent the ball but still it goes through it. Can anyone tell me the solution for this ? Thanks in advance.
Are you using transform.Translate to move the Ball? Because if you do then it goes through everything since there's no collision detection in transform.Translate
If you want collision you can use rigidbody, look up rigidbody in the Unity scripting reference
$$anonymous$$ore information needed: do you have a rigidbody/collider on your ball? How are you moving it? Assu$$anonymous$$g you have a floor, what did you do to prevent your ball falling through that?
http://docs.unity3d.com/Documentation/Components/class-$$anonymous$$eshCollider.html
Are your colliders convex?
$$anonymous$$y colliders are not convex. rigidbody/collider are set to ball. I am using rigidbody.AddForce to move the ball. When the ball moves slowly it works good, while the speed of the ball increases it goes through the wall. For the ball these values are set mass is 0.5, speed is 1000, bounciness is 0.8 and Bounciness Combine is maximum.
Well the values you set does not help us, it depends on the code you use. if it is too fast, even continuous collusion detection will not work. You have to make it slower or increase the size of the colliders. You could also program your own "detection". You could check ball distance to wall for example, i dont know if that works for your usecase.
Answer by swatatunity · Jul 26, 2017 at 07:50 PM
I was seeing the same issue and I was able to resolve it by setting "Size" parameters under "box collider" to 1,1,1 instead of 0,0,0 (default).
That worked for me, the default BoxCollider for a Unity 3D Cube object was (x:1,y:1,z:0). I changed the 'z' parameter to 1 and it works. Thanks mate !
Answer by jolo309 · Jan 08, 2014 at 09:20 AM
Are you using transform.Translate to move the Ball? Because if you do then it goes through everything since there's no collision detection in transform.Translate
If you want collision you can use rigidbody, look up rigidbody in the Unity scripting reference
I am using rigidbody.AddForce(). Here is my code
void FixedUpdate(){ Vector3 dir = Vector3.zero; dir.x = Input.acceleration.x; dir.z = Input.acceleration.y;
if (dir.sqr$$anonymous$$agnitude > 1) dir.Normalize();
dir = Time.deltaTime; rigidbody.AddForce (dir fSpeed); }
i have added sphere collider to ball and collision detection as continuous dynamic but still the ball passes through walls. For walls i have added box collider. Please give your suggestion.
Answer by emalb · Jan 08, 2014 at 01:07 PM
Maybe the collider on your ball or on the wall is set to Trigger.
Have a look at the Unity Roll-a-Ball tutorial if you haven't already. This provides an example of ball/wall collision in the 'Creating pick-up objects' video.
Edit for additional suggestions...
Try setting the Collision Detection drop down on your ball's Rigidbody to Continuous.
Have a look at the Unity reference material for Rigidbody for more detailed explantion.
I hope that helps!
The wall is not set to Trigger. When the ball moves slowly it works good, while the speed of the ball increases it goes through the wall.
Changed Collision Detection to Continuous but it didn't work. For the ball these values are set mass is 0.5, speed is 1000, bounciness is 0.8 and Bounciness Combine is maximum. This happens, when i move the ball very fast. I don't know why this is happening. Can u please help ?
How about you also answere to the other comments if you want help. It can have many different reasons why this does not work. One I said above, which is obsolete now cause you said it just happens if the ball is moving fast. How fast is it moving? If it is moving to fast its possible your collider is too thin or the ball is simply too fast for your method of collusion detection.
Answer by sath · Jan 09, 2014 at 07:18 AM
attach this script to your ball and handle the float values to your needs
Just test it
public float desiredSpeed = 5000f;
public float maximumDrag = 1f;
public float forceConstant = 5000f;
Rigidbody rigibody;
void Start(){
rigibody=transform.GetComponent<Rigidbody>();
}
void Update(){
Vector3 dir = Vector3.zero;
dir.x = Input.acceleration.x;
dir.z = Input.acceleration.y;
Vector3 forceDirection = new Vector3(dir.x, 0, dir.z);
//This reduces drag when the player adds input, and makes it stop faster.
rigidbody.drag = Mathf.Lerp(maximumDrag, 0, forceDirection.magnitude);
// this reduces the amount of force that acts on the object if it is already
// moving at speed.
float forceMultiplier = Mathf.Clamp01((desiredSpeed - rigidbody.velocity.magnitude) / desiredSpeed);
// now we actually perform the push
rigibody.AddForce(forceDirection * (forceMultiplier * Time.deltaTime * forceConstant));
}
Thanku for your help. i have tried this code but still the ball goes out when i move fast. i have tested with some float values like 1000, 2000, 500, 4000 etc. but goes out.
Your answer
Follow this Question
Related Questions
how to make a player die on collision 2 Answers
Stop ball rolling off wall but bouncing off 1 Answer
collision with wall so player dies 1 Answer
Wall jumping problem 1 Answer
Sphere through wall - Mini-Golf 5 Answers