- Home /
how to slow down speed increase?
I am using this script on a space ship, the negative values make the ship go in reverse. it works exactly as I want it to, except I want the speed to increase more slowly, I would like the speed to increase by 10 units a second. can someone give me some tips as to how I would achieve this
using UnityEngine;
using System.Collections;
public class SpeedControl : MonoBehaviour {
[SerializeField]
float speed = 2.0f;
public float trueSpeed = 0.0f;
public static string speedDisplay;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float power = Input.GetAxis("Power");
if (trueSpeed < 100 && trueSpeed > -30){
trueSpeed += power;
}
if (trueSpeed > 100){
trueSpeed = 99.99f;
}
if (trueSpeed < -30){
trueSpeed = -29.99f;
}
if (Input.GetKey("backspace")){
trueSpeed = 0;
}
speedDisplay = trueSpeed.ToString("0.0");
rigidbody.AddRelativeForce(0,0,trueSpeed*speed*Time.deltaTime);
}
}
sorry for being vague, im a bit new to all this, I have updated my original question with a more full question and the entire script
Answer by slay_mithos · Nov 06, 2014 at 07:57 AM
If you want the acceleration to be slower, but keeping the min/max speed, changing: float power = Input.GetAxis("Power");
into: float power = Input.GetAxis("Power")*0.5f;
Should half your speed increase.
This does not increase by 10 per second, because the whole point of the script is to handle the stick from a gamepad (the more you push it, the more it accelerates).
For a flat increase, you would want your "power" to use a hard number.
For a stable 10 per second, you would need to multiply 10 with Time.deltaTime (see http://docs.unity3d.com/ScriptReference/Time-deltaTime.html for an example)
no, that will work fine for what I need, thank you very much
Answer by b1gry4n · Nov 06, 2014 at 08:24 AM
Alternatively you could utilize : http://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html
float velocity = 1.0f;
float currentSpeed = 0.0f;
float maxSpeed = 100.0f;
float dampen = 25.0f;
void Update () {
currentSpeed = Mathf.SmoothDamp(currentSpeed, maxSpeed, ref velocity, Time.deltaTime * dampen);
Debug.Log(currentSpeed);
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Accelerate/Decelerate Custom Game Clock 2 Answers
Have a delay after each jump, so user cant spam jump 3 Answers
Do something every 0.5 seconds 3 Answers
Multiple Cars not working 1 Answer