- Home /
Ball Addforce Acceleration Speed
Hello, I am not too good with scripting in Unity and I have come across a few problems. I have tried for weeks to get it working but with no success.
Basically, I am trying to make a game where the player has to navigate a level using a wooden ball. They use the arrow keys to controll the rolling of the ball.
I have made a script using AddForce with Rigidbodies on my ball object but the ball rolls incredibly slow and takes ages to change direction.
I want to be able to have a fast acceleration and be able to change directions fast but still have a relatively low top speed.
Here is my script so far:
function FixedUpdate () {
if (Input.GetKey ("up"))
{
rigidbody.AddForce (Vector3.forward * Time.deltaTime * 5, ForceMode.Acceleration);
}
if (Input.GetKey ("down"))
{
rigidbody.AddForce (Vector3.forward * Time.deltaTime * 5, ForceMode.Acceleration);
}
if (Input.GetKey ("left"))
{
rigidbody.AddForce (Vector3.left * Time.deltaTime * 5, ForceMode.Acceleration);
}
if (Input.GetKey ("right"))
{
rigidbody.AddForce (Vector3.right * Time.deltaTime * 5, ForceMode.Acceleration);
}
I really need help with getting this script to work how I have described. Any help would be greatly appreciated.
Answer by syclamoth · Oct 07, 2011 at 04:02 PM
You're on the right track, except for some specifics-
- If you use ForceMode.Acceleration, there will be virtually no top speed. It's basically emulating gravity!
- There isn't really an inbuilt way to give a rigidbody a 'top speed' as such- you can, however, give it relatively high drag and low mass, and it will stop moving soon after you stop putting a force on it.
- Is there any reason why you need to be using discrete inputs here? Why can't you use the Input.GetAxis("Horizontal/Vertical")? If you do something like that, you could fake out your drag to give a better response time, and still control the desired velocity.
This is how I would do it:
public float desiredSpeed; public float maximumDrag; public float forceConstant; void Update() { Vector3 forceDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); //This reduces drag when the player adds input, and makes it stop faster. rigidbody.drag = Mathf.Lerp(maximumDrag, 0, forceDirection.magnitude); // this reduces the amount of force that acts on the object if it is already // moving at speed. float forceMultiplier = Mathf.Clamp01((desiredSpeed - rigidbody.velocity.magnitude) / desiredSpeed); // now we actually perform the push Rigidbody.AddForce(forceDirection * (forceMultiplier * Time.deltaTime * forceConstant));
}
Thanks for the tips. I have been trying to give it a high drag and have been nearly reaching the desired results I want but then gravity is really slow with a high drag.
I am not used to the C# language so I will learn it so I can understand better. I guess I should just work on my coding a little more before getting into this.
Your answer
Follow this Question
Related Questions
Ball physics help 2 Answers
How to reach a specific max speed with AddForce() ? 1 Answer
[Solved] Unity rigidbody.addForce ignores time.deltaTime? Possibly just a code bug I cannot find. 0 Answers
Rigidbody.AddForce 2d movement without acceleration 2 Answers
Confusion with AddForce with ForceMode.Acceleration 1 Answer