- Home /
Problem with bat hitting ball in table tennis game
I'm creating a table tennis game, where the bat is moved around with the mouse, and according to how fast the mouse is moved forward when contact is made with the ball, determines how fast the ball is hit.
Everything is going well, the ball and the bat collide properly if I keep the bat fairly still. But when I try to push the bat hard towards the ball, the ball seems to go through the bat or just slightly changes direction.
The ball has a 'bouncy' sphere collider, and has a rigidbody component attached. I have set the Collision Detection to Continous Dynamic.
The bat has a 'rubber' mesh collider, and has a rigidbody component attached. I have set the Collision Detection to Continous Dynamic.
On my bat I have a script to add force to the ball when the bat hits it... here is the script...
void FixedUpdate() { float h = horizontalSpeed * Input.GetAxis("Mouse X"); float v = verticalSpeed * Input.GetAxis("Mouse Y");
transform.Translate(v, h, 0);
}
void OnCollisionEnter(Collision theCollision)
{
if(theCollision.gameObject.name == "Ball")
{
theCollision.gameObject.rigidbody.AddForce(horizontalSpeed * 10, 0, verticalSpeed);
Debug.Log("Hit the bat");
}
}
Not "an answer", but can you try increasing physics iteration solver count in physics manager?
Also, I doubt your bat code is any continuous since you use transform.Translate.
I tried increasing the solver count to various values but unfortunately I still get strange behaviour with the bat hitting the ball. As in the below post I have changed to use rigibody.$$anonymous$$ovePosition.
Answer by Mike 3 · Dec 27, 2010 at 01:28 AM
You need a rigidbody on your bat, and you need to move the position using physics. At the very least, make the rigidbody kinematic, and use:
rigidbody.MovePosition(rigidbody.position + transform.rotation * new Vector3(v, h, 0));
Thanks for the answer, although it seems sightly better, the ball still goes through the bat when I try take a good swing, it either goes through or comes off at a strange angle.
I already had a rigidbody on my bat and had it as kinematic. Anything else I can look at?
Thanks
Next step - try changing the physics timestep in Time settings
Thanks $$anonymous$$ike, changing Fixed TimeStep in the Time $$anonymous$$anager to 0.001 eventually did the trick :) Doesn't this value seem extremely low?
yeah, you're doing physics 1000 times a second, generally it's 50-100. also, try using primitive colliders for the bat (a cube collider for the main part for example), may help a lot
Your answer
Follow this Question
Related Questions
Enemy bouncing physics 0 Answers
Ball jump on collision problem 1 Answer
Simple Collision Deflection (Pong) 1 Answer
Moving a ball around a maze 3 Answers
Change direction of ball based on where it hits paddle -- 3d breakout game 0 Answers