- Home /
add force get stronger over time slow.
i am making a car and i need it to speed up slowly so i have tried putting a * time.deltatime but that didn't work, i did make one huge script that had like 20 if staments so that every time the force would get stroger, but this was a pain because it take a lot of work to do a little tweak so how could i make this better.
var gravity = 20000;
var GroundGravity = 1000;
var Forward = 10000;
var reverse = 9000;
var speed : float;
var Car: Transform;
function FixedUpdate () {
speed = rigidbody.velocity.magnitude;
Time.timeScale = 1;
if(groundtrigger.triggered==0)
{
rigidbody.AddForce(Vector3.up * -gravity);
}
if(groundtrigger.triggered==1)
{
rigidbody.AddForce(Vector3.up * -GroundGravity);
}
if(Input.GetKey("w")&& (groundtrigger.triggered==1))
{
Car.rigidbody.AddForce(transform.forward * Forward);
}
if(Input.GetKey("s") && (groundtrigger.triggered==1) && (speed <50))
{
rigidbody.AddForce(Car.transform.forward * -reverse);
}
if(Input.GetKey("d") && (groundtrigger.triggered==1) && (speed > 1))
{
rigidbody.AddTorque(Car.transform.up * turnSpeed);
}
if(Input.GetKey("a") && (groundtrigger.triggered==1) && (speed > 1))
{
rigidbody.AddTorque(Car.transform.up * -turnSpeed);
}
}
Force, in Unity, is representative of the acceleration of a rigidbody in motion. When you say you want to increase the force over time, you're basically saying you want the acceleration of an object in motion to change over time, or more specifically, you want the object to have a constant non-zero jerk. While limiting and manipulating the jerk of a moving object has various applications in real life, there is really no practical reason to touch it in a video game. If you were building a simulation for NASA, then I would be inclined to believe that you would need access to the jerk, but if that was the case, you wouldn't be using a free game engine to achieve your purposes.
I believe what you really want is to give the object a constant acceleration so that its velocity would gradually increase or decrease depending on the situation. The thing is, AddForce already does that. By applying a constant amount of force (which like I said is representative of acceleration) every frame, the velocity of an object gradually changes. You just need to know when and how to effectively apply that force to fulfill your objective.
Answer by SilverTabby · Jul 10, 2011 at 05:29 AM
This is what I would do to add force over time
if(Input.GetKey("w")&& (groundtrigger.triggered==1))
{
Car.rigidbody.AddForce(Car.transform.forward * Forward * Time.deltaTime, ForceMode.Acceleration);
}
The Key diffrence is the ForceMode.Acceleration
. This makes it so that the force is added in the exact same way that gravity wroks: a constant increase in speed.
Keep in mind that acceleration IGNORES the mass of the rigid body, so be careful with using it
If you wan't to not ignore the mass you could use this
Car.rigidbody.AddForce(Car.transform.forward * Forward * Time.deltaTime / rigidbody.mass, Force$$anonymous$$ode.Acceleration);
because acceleration = force / mass.
$$anonymous$$
i tried both but it still accelerates to fast and if i make the force lower then it has a very low maximum speed i have been confused on this for a longtime i want it to accelerate SLOWLY like a real car this will get from 0 - 60 in a half a second?
@Ninjaoboy You are applying a force of 10000 units/sec to your car. What do you expect?
Answer by kdubb · Jul 10, 2011 at 04:49 PM
The answer lies in rigidbody.AddForce. As stated in another answer you can use one of the ForceMode's to choose whether the value ignores, or pays attention to, the mass of the rigid body.
Any rigid body that you accelerate will do so with respect to newtonian dynamics (aka "physics"). This means that a rigid body with low mass and zero drag will accelerate almost instantaneously; coincidentally these are the defaults in Unity's rigid body component. So to control the speed of acceleration of the rigid body you need to properly set its mass and drag. Try real world settings for these and play with the numbers from there.
The final thing to take into account is the friction involved. Unity uses physics materials to specify the friction properties of colliders (among other things). These friction properties will also affect the rigid body's acceleration if they are set and actively colliding with any of the rigid body's own colliders. One caveat though is that Unity's WheelColliders ignore these friction properties and use an entirely separate friction model specified in the collider properties (look it up in Unity's documentation if you are using these).
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do I make a car in unity? 1 Answer
WheelColliders Bug Fix 0 Answers
How to set max speed for this car ? 1 Answer
if moving statement 1 Answer