Increase Speed in Gameplay

I’m making a Space Sim. I need to have my ship in constant motion forward. I have that with my script, so I need to make it so when you hit the up key, it will progressively make the ship move faster, and make it go progressively slower when hitting the down key. It also has to have a limited amount of speed. Here’s my script:

var Speed = 50;
var Player = transform;     

function Update() 
{
    transform.Translate(Vector3.forward * Time.deltaTime * Speed);
    
    if(Input.GetKey("up"))
    Player.rigidbody.AddForce(transform.forward * 10);
    
    if(Input.GetKey("down"))
    Player.rigidbody.AddForce(transform.forward * -10);
}

Several things are wrong with this. I’m getting the error message:

Screen position out of view frustum (screen pos 917.000000, 646.000000) Camera rect 0 0 1552 723)

…when I hit the up key or the down key. And I get this error message right after that:

!!sNormalized (dir)

so now I’m completely and utterly confused. If you could, that would be appreciated. Thanks.

You need to add a few variables. Apologies if I don’t get the syntax exactly right – I’m more of a C# guy:

var defaultSpeed = 50.0; // speed to default towards if no keys are pressed
var maxSpeed = 75.0; 
var minSpeed = 25.0;
var maxAcceleration = 5.0; // Controls the acceleration
var currentSpeed = 50.0;

// var Player = transform;  // this line gave me an error and it isn't needed any way

function Update() 
{
    transform.Translate(Vector3.forward * Time.deltaTime * currentSpeed);

    if(Input.GetKey("up"))
    currentSpeed += maxAcceleration * Time.deltaTime;
    else if(Input.GetKey("down"))
    currentSpeed-=maxAcceleration * Time.deltaTime;
    else if (currentSpeed > defaultSpeed){
        currentSpeed -= maxAcceleration * Time.deltaTime;
        currentSpeed = Mathf.Clamp(currentSpeed, defaultSpeed, maxSpeed);
    }
    else {
        currentSpeed += maxAcceleration * Time.deltaTime;
        currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, defaultSpeed);
    }
    currentSpeed = Mathf.Clamp(currentSpeed, minSpeed, maxSpeed);
}

A little sloppy, but I believe that this is what you’re asking for.

Using Translate and AddForce are two completely different parts of the Unity engine. One uses the physics engine and the other uses the transform engine. You can get the same effect but there not interchangeable.

You've got to re-write your script so when you press the up key it simply increases the variable "speed" and when you press the down key it decreases the variable "speed".

var Speed : int = 50;
var FastSpeed : int = 75;
var SlowSpeed : int = 25;
var Player = transform;     

function Update() 
{
    transform.Translate(Vector3.forward * Time.deltaTime * Speed);

    if (Input.GetKeyDown ("up")){
    Speed = FastSpeed;
}
    if(Input.GetKeyDown ("down")){
    Speed = SlowSpeed;
}
if (Input.GetKeyUp ("up")){
FastSpeed = Speed;
}
if (Input.GetKeyUp ("down")){
SlowSpeed = Speed;
}
}

ok i have worked on a game like this before and here is how i did it.

var curSpeed = 10.0;

function Update()

{

   rigidbody.AddForce(transform.forward * Time.deltaTime * curSpeed);

   if(Input.GetKey("up"))

   {

       curSpeed += 1;

   }

   if(Input.GetKey("down"))

   {

       curSpeed -= 1;

   }

}

I understand this is an older thread, but wanted to add a few comments (my two cents). Variables are not that expensive, and good code should use them if it makes the script more versatile. For instance, having a max speed and min speed variable (min speed = 0 or less if you want to fly backward). This way as the player presses forward, the currentSpeed variable can move within the constraints of min and max speed. Also, a variable called lastSpeed can be used to save currentSpeed when a player triggers boost. This way when the boost is over the speed can adjust back to the value it held previous to the boost. You asked about gradual increase and decrease in speed. This can be accomplished using the Mathf.Lerp().

Are you also transforming your camera position with the ship movement?