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");
}
}