Physics Question (453558)

Hi, i am trying to make something that works like: a bat (the bat does not have a rigidbody attached to it) hits the ball and it applies force to the rigidbody of the ball in the correct direction at the speed of how hard i hit it. I imagine this would involve CollisionEnter, magnitude, and angle stuff, but i just can’t figure out how to go about writing this script. Could anybody help?

Bump

Please try to wait at least 24 hours before a bump.

Yes, it would involve OnCollisionEnter.
Search the scripting reference for it, and look at its parameter (the Collision). It has tons of useful information, such as the contacts and normals.

I know sorry, it’s just that this script is like the most important in the game and I really need it. So how would you do an addforce to the object that you collided with?

In the Collision parameter of OnCollisionEnter, you get access to the Rigidbody of the object that collided with it.

function OnCollisionEnter (collision : Collision) {
    collision.rigidbody.AddForce (direction*force);
}

No I mean like how to acess the ball from the bat (wilhich doesn’t have a rigidbody)

I just told you how to access the ball from the bat.

OnCollisionEnter only requires that one of the two objects has a rigidbody, but the function can be used on either object.

collision.rigidbody is the rigidbody of the ball.

Oh ok sorry, I think I get it now. So then how would you tell it to add the force in the direction it was hit?

In the Collision parameter are the contact points and the relativeVelocity (though in this case it is really the balls velocity because the bat has none)

You put them together and you get something like this

function OnCollisionEnter (collision : Collision) {
    collision.rigidbody.AddForce (collision.contacts[0].normal*collision.relativeVelocity.magnitude);
}

That brings up an error “Operator ‘*’ cannot be used with a left hand side of type ‘UnityEngine.Vector3’ and a right hand side of type ‘UnityEngine.Vector3’.”

There was a small mistake which I have fixed.

The code I posted isn’t the code you will be finished with, it was a jumping off point, you shouldn’t try to copy it word for word.

Does the bat have a collider? Just because an object doesn’t have a rigidbody component doesn’t mean that it can’t affect other rigidbodies. As long as it has a collider, it should be able to hit the ball and unity will take care of the Physics calculations.

It does have a few colliders on it and I found out that it can interact with the balls but its not doing it correctly. Its like the bat tries to go around the ball and it only makes the balls move a very small amount.

Is the bat moving? you might want to try increasing the bounciness of the ball.

Yeah its actually not a bat, its supposed to be a drum stick hitting a bunch of pool balls (its a game me and my friends made up and i’m trying to make a video game of it). Also i have the physic material set to bouncy, but i think that might actually be part of the problem because if i set the collision detection to continuous, it just starts shaking and bouncing

If its moving you should probably attach a rigidbody to it, otherwise weird stuff will happen, just turn off the gravity.

Ok so i put the rigidbody on it, added a TON of colliders to the drum stick then duplicated the drum stick to get 2 times the colliders. So now it is working and the balls are moving decently, but it is EXTREMELY sensitive