• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Meltdown · Dec 26, 2010 at 08:18 PM · collisionphysicsball

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Dec 26, 2010 at 09:49 PM 0
Share

Not "an answer", but can you try increasing physics iteration solver count in physics manager?

avatar image Statement · Dec 26, 2010 at 09:50 PM 0
Share

Also, I doubt your bat code is any continuous since you use transform.Translate.

avatar image Meltdown · Dec 27, 2010 at 08:35 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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));
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Meltdown · Dec 27, 2010 at 08:22 AM 0
Share

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

avatar image Mike 3 · Dec 27, 2010 at 11:30 AM 0
Share

Next step - try changing the physics timestep in Time settings

avatar image Meltdown · Dec 28, 2010 at 07:24 AM 1
Share

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?

avatar image Mike 3 · Dec 28, 2010 at 07:30 AM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Moving a ball around a maze 3 Answers

Change direction of ball based on where it hits paddle -- 3d breakout game 0 Answers

Enemy bouncing physics 0 Answers

Simple Collision Deflection (Pong) 1 Answer

Ball jump on collision problem 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges