How do I make an object move using the Up arrow? I’m using this code atm:
function Update () {
Input.GetKeyDown(“up”);
transform.Translate(.001, 0, 0);
}
But, when I go to play, it moves without me pressing it:face_with_spiral_eyes: .And I don’t want the object to move in only 1 direction, because it is the player. Any help is appreciated.
About as simple as it can be made for keyboard only inputs. Just setup your keys in the inspector to include Forward, Reverse, Turn Left, Turn Right. Hope it helps
I figured it out. The variables were in the inspector. Thanks for all your guys help. Last question, How do I make the object move at an increasing rate? Instead of 1 speed?
Thats more complicated… This is by no means the only way to do this but it’s what i’ve come up with.
This will only get you to move forward at an accelerated rate you can add the rest.
var MaxSpeed : int = 5; //how fast do you want to go.
var CurSpeed : float = 0.0; //DO NOT CHANGE..
var AccelRate : float = 0.008; //how fast do you want to accelerate
var MaxAccel : float = .03; //what do you want your max acceleration to be...
var CurAccel : float = 0.0; //DO NOT CHANGE
var Deccel : float = 0.008; //how fast do you want to stop
function Update () {
if (CurSpeed !=0 !Input.GetButton("Forward") !Input.GetButton("Reverse") ){
CurSpeed = Mathf.Lerp(CurSpeed, 0, Time.time * Deccel);
CurAccel = 0;
}else{
if (CurSpeed < MaxSpeed Input.GetButton("Forward")) {
if (CurAccel < MaxAccel){
CurAccel = Mathf.Lerp(CurAccel, MaxAccel, Time.time * AccelRate);
}
CurSpeed += CurAccel;
}else{
if (CurSpeed == MaxSpeed Input.GetButton("Forward")){
CurSpeed = MaxSpeed;
}
}
}
transform.Translate(Vector3.forward * Time.deltaTime * CurSpeed, Space.Self);
}
It may look messy in the forum. Thats because of the way it’s handling word wrapping and spacing. Just copy it into you javascript and it will look better.
Thanks. It works. But, when I edit the Speed var, it only goes up to that speed. I want to make the car slower, so I edited that. But, How can I make it go faster than the Speed var?
EDIT: Just saw the newest reply. Let me read that.
EDIT2: I get an error: …'expecting }, found ". And, can you explain to me what all that code means? It’ll help me learn easier.
Double post. I got it to be slower. But, There’s 2 things that I dont like. Is there a way to not make it rotate when in a stationary position? And, is there a way to make it brake faster? That’s all i need. Thanks
I’m assuming you’re trying to simulate a car or other similar vehicle?
If so, a simple (non-physics-based) solution would be to make the rate of rotation proportional to the vehicle’s linear speed. (In other words, the faster it’s moving, the faster it can turn.)
If you want to be a little more realistic, you can compute the maximum rotation based on the current linear speed and the vehicle’s turn radius.