Update() is for per-frame things, but the engine uses FixedUpdate() for physics stuff.
Physics generally run better with a fixed update independent of the framerate. I run my scenes at 500hz physics currently.
Unity also has the rigidbody component where you can apply forces and torques to you objects. This is a good API to learn since it’s the best way to do this stuff.
you should set up a max and min speed. where max speed could be 10 or 100. from this you simply would do this.
var Speed = 0;
var minSpeed : float= 0;
var maxSpeed : float = 10;
var Accel = maxSpeed / .4; //change this to set up how much you want to speed up and down.
what this does is adds your accel to the speed by what you specify. check my math though im not very good at it. i basicly figured this out by playing with differnt settings.
after you get this set up your speed should look somethig like this.
this should help you out i cut out some things and changed them from my original script to make more sence to you in your project. i would not sugest copy and pasteing it as i am probly useing differnt var’s and differnt key names. but this should be what your looking for. its a start. its also rigid body dependent so your going to need a rigid body controler on your mesh.
also you should be aware that this code doesnt really decellerate properly it stops you abruptly. i am working on this as i have yet to get it working properly. too busy doing what im good at… modeling.
but i will be revisting this script soon.
First I tested the script from zemog86
this is my setup so you have a engine script i separated it from a navigation script for rotation
var speed = 0;
var maxSpeed = 200;
var minSpeed = -20;
function Update () {
print ("Actual Speed: " + speed);
// higher movespeed with w and up key
if(Input.GetKey("up"))
{
if(speed <= maxSpeed)
speed += 1;
else
speed = maxSpeed;
}
// lower movespeed with s and down key
else if(Input.GetKey("down"))
{
if(speed >= minSpeed)
speed -= 3;
}
// lower movespeed if no key is pressed
else if(!Input.GetKey("up"))
{
if(speed >= minSpeed)
speed -= 2;
}
transform.Translate(Vector3.forward * Time.deltaTime * speed /10);
}
Works ok, only Problem is: if you stop the aceleration goes to -1 or -2, so you move a little backwards
Next i tried your script orindoomhammer
I gues i did something wrong, this is my setup
var Speed = 0;
var minSpeed : float= 0;
var maxSpeed : float = 100;
var Accel = maxSpeed / .4;
//change this to set up how much you want to speed up and down.
function Update () {
print ("Actual Speed: " + Speed);
if(Input.GetButton("Acellerate")){
Speed = Accel + Mathf.Clamp(Speed, minSpeed, maxSpeed - Accel);
}
if (Input.GetButton("Decellerate")){
Speed = -Accel + Mathf.Clamp(Speed, minSpeed +Accel, maxSpeed );}
if(("Acellerate")){
rigidbody.velocity = transform.forward * Time.deltaTime * Speed;
//Apply velocity in Update()
}
}
Because when you hit the Button it just jumps to maxspeed and keeps it up. It does not acellerate and not decellerate if you release the Button.
Thanks a lot, lots of stuff to keep me um experimenting on
told you it would be buggy. its my first script. the reason i have it set to /4 on my script is so you can scale the speed of each ship just by setting a max speed. workd pretty slick on my main code. but still has some bugs to work out.
I experienced a problem of accelearting rigidbody as a bug, may appear interesting for you (note that I’m writing it on the fly, probaably won’t work but get the main idea):
var maxSpeed = 10.0;
var accelerationForce = 1.0;
function Update ()
{
if(Input.GetKey("Accelerate"){
{
if(rigidbody.velocity >= maxVelocity)
rigidbody.velocity = maxVelocity;
else
rigidbody.AddForce(accelerationForce);
}
}