- Home /
Ball hitting physics?
Hello,
I have been using Unity for a few weeks, and have begun to script my own small baseball game. It consists of just an automatic pitch and the player will have to hit it by clicking the mouse at the right time. To break it down.
The balls co-ordinates are contained in variables.
The user clicks when the ball is close.
If the cursor location on click is close to the ball (I use mathf.clamp to compare cursor location to the co-ordinate variables).
A hit is registered. Velocity is added to the ball relative to the bat used.
For now I want to add more realistic hits to the ball. I plan to do this with multiple mathf.clamp scripts, adding differing velocity and torque to the ball based on where the click was in relation to the ball.
Before I start this very long process, I would like to know if there is an easier way to add more realistic ball paths when the ball is hit?
hope I have explained everything clearly. I am still quite new with Unity and I am not looking for script examples, just other possible methods. Thanks for any help!*
A couple of musings:
Rather than apply force by hand, I wonder how well it would work if you allowed the ball to hit a collider. The collider does not have to be visible. A sphere or a capsule might work. The collider could be stationary or you could move it forward to simulate the hit. If necessary, just after they lose contact, you could magnify the velocity. You could size the collider appropriate to how "wild" you want the hits to be.
With the angular velocity and axis of rotation, you can calculate the vector for applying a curve force. Then you can apply a small amount of force each Update() to that vector to get the ball to curve. This can be used for both pitches and hits.