Ball through bat collision problems!

This question has been asked a lot of times and there are a variety of replies and answers but none of them have helped me solve this problem (I have looked, and Googled and downloaded scripts honest):

I have a ball (sphere) with a collider and rigid body.
I have a bat (a 1 x 3 cube) with a collider and a script that makes it rotate on the Y axis and return after the M key is pressed/released - so it just rotates back and forth. The idea is for the ball to be knoced away when the ball hits the bat and you press M. They collide and the ball stops if it hits the stationary bat. But when I press M the bat simply goes through the ball and returns to the start position . I have read up and I understand about changing the collision detection and the timestep and I have uploaded the DontGoThroughThings csharp script.

I think I’ve done all things I can think of to get the bat to hit the ball but alas no joy.

Can anyone offer a solution or idea?

p.s If I fire the ball at a stationary bat it stops it fine. No matter how fast or hard I fire the ball, yet a stationary ball sitting next to the bat will go through the bat! The bat rotates at .2 seconds - if I lower this to say .5 or 1 second it seems to work but I need a fast batting action not a slow-mo bat!

thanks

So have you met all the expected conditions for proper physics handling… ie :

Does you bat have a rigedbody attached? Are you moving the rigidbody? Are you handling this code within fixedupdate?

You should never directly move the transform component for anything to work with physics properly.

The ball has a rigid body but the bat does not. If I attach a rigid body to the bat then all hell breaks loose when I click M :slight_smile:

I assume that only the ball should have a rigid body? It bounces of the sides and other obstacles and even bounces off the bat but upon triggering M the bat and ball refuse to… play ball!

So… In a scenario… you create a tennis game and you want the ball to bounce on the racket as the racket is swung? How?

thanks

Sure the ball can interact with static colliders that dont have rigidbodies … because they are not moving - but you are moving the bat so it requires a rigidbody to work “properly” with physics. The bat would probably also need some sort of restraining joint obviously

Yeah, tried all that. It seems that any and everything I’ve tried has failed. I have created a work-around but it’s not ideal and still results in collision anomalies but for the moment it allows me to continue with the project. I will have to keep searching for a solution.

Surely there must have been games created in Unity that rely on a bat and a ball communicating properly?

Of coarse there are , I dont know why you mention needing to keep searching for a solution, you just need to figure out what you are doing incorrectly. I gave you the check list above , if it moves it must have a rigidbody , you never manipulate the transform directly for a component with a rigidbody, use the proper methods of moving rigidbodies, only do such movement within fixedupdate.

I can assure you personally there is no problem with “bat and ball” since I have made pinball tables in the past using Unity 4x and am doing a new pinball table in unity 5x. One thing to mention for unity 5 and hinge joints with motors if you require large force/velocity values you need to lower the global fixedtime step of your project… otherwise the joints become unstable.

Yep you’re making a complete pigs ear of things. When you accept that, you will be able to accept help to solve it.

Thanks for the replies. I shall endeavour to nail the problem. I am a novice and the solution is most likely staring me in the face. The problem is my own short comings.
thanks