- Home /
Acceleration script
How do I change this script so that it has simple acceleration? I you have a few extra minutes, it'd be nice to explain. :)
//Basic force to move a rigidbody object
var power : float = 500.0;
function Update () {
if(Input.GetKeyDown(UpArrow)){
rigidbody.AddForce(Vector3(0,0,power));
}
Answer by asafsitner · Aug 13, 2013 at 09:45 PM
From **Wikipedia**, acceleration means that an object moves progressively faster.
This means that instead of feeding an object a fixed velocity, you increase the velocity over time. For example:
//Basic acceleration
var velocity : float = 10.0;
var acceleration : float = 10.0;
function Update ()
{
//this line adds the acceleration value to the power value over the course of 1 second, up to infinity
power = power + (Time.deltaTime * acceleration);
//this line feeds the ever-growing force to the object, propelling it faster and faster over time
transform.Translate(0, 0, power);
}
You can do something a little more advanced, such as declaring an initial velocity and a final (terminal) velocity and even a deceleration rate.
This is a little more complex example:
//Acceleration example, Level 2
//this is our target velocity while decelerating
var initialVelocity : float = 0.0;
//this is our target velocity while accelerating
var finalVelocity : float = 500.0;
//this is our current velocity
var currentVelocity : float = 0.0;
//this is the velocity we add each second while accelerating
var accelerationRate : float = 10.0;
//this is the velocity we subtract each second while decelerating
var decelerationRate : float = 50.0;
//movement happens here
function Update ()
{
if(Input.GetKeyDown(UpArrow))
{
//add to the current velocity according while accelerating
currentVelocity = currentVelocity + (accelerationRate * Time.deltaTime);
}
else
{
//subtract from the current velocity while decelerating
currentVelocity = currentVelocity - (decelerationRate * Time.deltaTime);
}
//ensure the velocity never goes out of the initial/final boundaries
currentVelocity = Mathf.Clamp(currentVelocity, initialVelocity, finalVelocity);
//propel the object forward
transform.Translate(0, 0, power));
}
And so on.
Answer by PointyPigheadGames · Oct 13, 2016 at 05:05 AM
Here's the script I use:
//Acceleration example, Level 2
//this is our target velocity while decelerating
var initialVelocity : float = 0.0;
//this is our target velocity while accelerating
var finalVelocity : float = 500.0;
//this is our current velocity
var currentVelocity : float = 0.0;
//this is the velocity we add each second while accelerating
var accelerationRate : float = 10.0;
//this is the velocity we subtract each second while decelerating
var decelerationRate : float = 50.0;
//movement happens here
function Update ()
{
if(Input.GetKey(KeyCode.Space))
{
//add to the current velocity according while accelerating
currentVelocity = currentVelocity + (accelerationRate * Time.deltaTime);
transform.Translate(0, 0, currentVelocity);
}
else
{
//subtract from the current velocity while decelerating
currentVelocity = currentVelocity - (decelerationRate * Time.deltaTime);
if(currentVelocity > 0) {
transform.Translate(0, 0, currentVelocity);
}
else {
transform.Translate(0, 0, 0);
}
}
//ensure the velocity never goes out of the initial/final boundaries
currentVelocity = Mathf.Clamp(currentVelocity, initialVelocity, finalVelocity);
//propel the object forward
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help on a basic script 2 Answers
Help for basic board game but for me it's hard.. 0 Answers