- Home /
Ball hits bat or any rigidbody...turn off force...
Hello, I help special needs children focus...
I am trying to prove the concept of a pitcher / batter experience to help some of the wheelchair children "experience" athletics. I've scoured the answers and forums, and can get some basic physics going well on the effort. The problem is that I haven't yet discovered how to turn off the initial velocity of the pitchspeed...
var pitchspeed : float; var collidedwith : boolean;
pitchspeed = -45;
function Update() { if(collidedwith != true) rigidbody.AddForce (0, 0, pitchspeed); }
function OnCollisionEnter(collision : Collision) { collidedwith = true; rigidbody.velocity = -rigidbody.velocity;
}
Sincere thanks
Answer by skovacs1 · Aug 25, 2010 at 10:47 PM
First, consider what your code does.
//Every Frame - Physics are meant to be done in FixedUpdate() - See the docs function Update() { //when you haven't collided with anything, add more force? //what's pushing the ball after it gets thrown? if(collidedwith != true) rigidbody.AddForce (0, 0, pitchspeed); }
//When there's a collision function OnCollisionEnter(collision : Collision) { collidedwith = true; //Set it to always move at the same speed in the opposite direction //I don't think that a bat hitting a ball will hit it back with the same speed. rigidbody.velocity = -rigidbody.velocity; }
You probably want the ball to slow down after being thrown. You probably also want your bat to hit the ball with a certain speed, but to have the ball slow down in the air. Setting the velocity makes it always have that velocity unless you apply something to change that. Forces are also constant. What you likely want is drag - drag will slow down rigidbody motion.
What it seems like you want is:
- When you throw the ball (in Start() is fine for your tests), add an appropriate force or set the velocity to represent the pitch speed. This could be calculated based on some throwing animation or whatever. Note that to add spin, you would add torque (likely with angular drag) and you might consider adding a slight relative force to get the ball to actually spin through the air.
- When the bat hits the ball, add an appropriate force or set the velocity appropriately relative to the mass, speed and direction of the bat. Using AddForceAtPosition with the appropriately calculated force can provide a very realistic approximation. Also bear in mind that if the bat is moving fast enough or the ball slow enough, it will carry the ball for a time, changing the equation some. -Time to pull out those junior high/high school physics notes or start googling (try wikipedia).
As an alternative, if you really want to use the physics, then why not use them for all of it? Have both the ball and the bat have rigidbodies with appropriate mass and drag settings, etc. If you apply the appropriate forces, velocities, torque, etc. you should get a realistic approximation of how physics would treat these objects. Bear in mind that the bat's motion may be a bit deterministic with this approach depending on how you do it.
Thank you very much for the in depth plain english! I will keep going on this and let you know how it unfolds. Sincere Gratitude for the effort!!!