- Home /
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.
What I mean by a limited amount of speed is that the player can speed up to a certain number, or slow down to a certain number.
Answer by redlotus · Aug 17, 2011 at 02:07 AM
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.
Thank you. This almost works. There are no errors (so your syntax is fine), and I can lower the speed by a certain amount, but I can't speed up. Plus, no matter how much I change the speed variables, nothing changes. I'm not sure why.
Sorry, but I forgot to make all of the variables floats. I've edited the script and you should be able to speed up/slow down correctly.
As for changing the speed variables, are you changing them in the inspector or the script?
Yeah, my bad, I was fixing them in the script. You're right. I'm sorry. Thank you. Perfect, it works.
Answer by FTheCloud · Aug 15, 2011 at 05:34 PM
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; } }
Well, this answered one of my other questions, how to boost. Ha. Well thanks because now my ship has a boost, but I need the speed to slowly increase, not have fixed speeds. Thanks anyway.
Answer by DGArtistsInc · Aug 15, 2011 at 05:30 PM
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;
}
}
The problem with this script is it doesn't reset the speed to what it was before the user pressed the key and every time you press up or down it constantly gets faster and faster and vice versa.
Now, when the game starts, I don't have to press anything. The screen doesn't show anything and I get this error message:
Screen position out of view frustum (screen pos 906.000000, 687.000000) Camera rect 0 0 1428 717)
nighthawx349 are you putting this script on the camera or your ship?
Answer by Pwest01 · Apr 03, 2012 at 11:51 PM
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?
Yes, the camera is transfor$$anonymous$$g with the ship. And yes thanks for that. I haven't looked into $$anonymous$$athf.Lerp() completely. What does it do exactly?
Your answer
Follow this Question
Related Questions
Need help with 2D Flight Simulator 0 Answers
Flight HUD - flight angles 0 Answers
Flight physics 0 Answers
JSBSim and Unity 0 Answers