How to move an object without impacting it's natural gravitational behavior
Hi — I'm working on a concept where player controls the ball's forward motion. However, when the ball climbs a certain height, it doesn't drop naturally due to the forward velocity—it literally flies until gravity slowly brings it to the ground.
So instead I've considered something like this (below) where instead of controlling the ball velocity, the player controls a hollow block that traps the ball inside it and pushes it forward:
The behavior is exactly what I want, but unfortunately, this doesn't work well the because the ball keeps escaping the collider. I've tried dynamic continuous collision with/without rigid body for the block— no luck.
I'd love to hear if there is a more clever way of doing this. Perhaps something less expensive that it doesn't rely on collisions with a "pusher" object. Any idea/feedback is appreciated.
Answer by CodesCove · Dec 08, 2021 at 02:56 PM
Try using Physics.AddForce. Pseudo code example
rb.AddForce(Vector3.forward * 300 * Time.deltaTime, ForceMode.Force);
Where Vector3.forward is the world space z-axis direction (or what ever direction you define) and 300 is the magnitude or amount of pushing force. Time.deltaTime makes sure the force will be constant over time. ForceMode.Force will apply the force like you were pushing the object.
Run it inside FixedUpdate()
Note the ForceMode.Force will take in to account the mass of the pushed object so you may need to adjust the force accordingly.
https://docs.unity3d.com/ScriptReference/ForceMode.html
https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Thank you so much! Exactly what I needed! Like you said, I had to tinker with mass and drag so that the ball doesn't keep on gaining speed while in mid-air.
Cheers!
Your answer
Follow this Question
Related Questions
Make Ball roll without imput 0 Answers
3D Trigger collider slows player moved by forces 0 Answers
How to make a ball bounce always to the same height? 3 Answers
How do I make a ball have force where it bounces and adds force in the direction it is going at? 0 Answers
How to fix minor jitter/stutter when moving a 2D character using physics in FixedUpdate() 0 Answers