- Home /
Airplane smooth acceleration problem
Hello! I'm new to Unity. I make a airplane, using a rigid body, the script is working correctly. But I cannot do airplane is smoothly increased from the current speed to the maximum and achieve the same slowing down (time is now or instant acceleration or deceleration). I tried to do through Rigidbody.velocity, but it does not work. I beg you with a code or advice on how to achieve a gradual acceleration and deceleration.
Thank you in advance!
Here is my script:
var Speed = 60;
var boostSpeed : float = 10;
var maxSpeed = 100;
private var Amount : Vector3 = Vector3 (10, 0, 0);
function FixedUpdate () {
//Roll
if (Input.GetKey(KeyCode.LeftArrow)) {
rigidbody.AddRelativeTorque(Vector3.forward * 7 + Amount * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.RightArrow))
rigidbody.AddRelativeTorque(Vector3.back * 7 + Amount * Time.deltaTime);
//Forward current speed
transform.Translate(Vector3.forward * Time.deltaTime * Speed);
//Boost-Brake <--- PROBLEM
if (Speed < maxSpeed && Input.GetKey(KeyCode.W)) {
rigidbody.AddRelativeForce(Vector3.forward * 70 + Amount * Time.deltaTime);
}
else if (Speed <= maxSpeed && Input.GetKey(KeyCode.S))
rigidbody.AddRelativeForce(Vector3.forward * -20 + Amount * Time.deltaTime);
//Yaw
if (Input.GetKey(KeyCode.D)) {
rigidbody.AddRelativeTorque(Vector3.up * 3 + Amount * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.A))
rigidbody.AddRelativeTorque(Vector3.down * 3 + Amount * Time.deltaTime);
//Pitch
if (Input.GetKey(KeyCode.UpArrow)) {
rigidbody.AddRelativeTorque(Vector3.right * 5 + Amount * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.DownArrow))
rigidbody.AddRelativeTorque(Vector3.left * 5 + Amount * Time.deltaTime);
}
if deltatime is 0,01
Vector3.forward 70 + Amount Time.deltaTime = 0,0,1 70 + 10,0,0 0.01 = 0,0,70 + 0.1,0,0 = 0.1,0,70
your breaking is 0.1,0,-20
Don't know what Amount is supposed to do, but you should probably multiply the deltatime with the "Vector3.forward 70" also. (Vector3.forward 70 + Amount) * Time.deltaTime
I didn't understand a bit how to do what you have written. I am new to Unity. Is so:
//Boost-Brake <--- PROBLE$$anonymous$$
if (Speed < maxSpeed && Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) {
rigidbody.AddRelativeForce((Vector3.forward 70 + Amount) Time.deltaTime); }
else if (Speed <= maxSpeed && Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
rigidbody.AddRelativeForce((Vector3.forward -20 + Amount) Time.deltaTime);
It does not work.
I do not know how to write it?
Vector3.forward 70 + Amount Time.deltaTime = 0,0,1 70 + 10,0,0 0.01 = 0,0,70 + 0.1,0,0 = 0.1,0,70 your breaking is 0.1,0,-20