Making an object move

Hello,

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. :wink:

Try this:

if (Input.GetKey(KeyCode.UpArrow)) {
	 transform.Translate(Vector3.forward * Time.deltaTime);
}

Cool! Thanks earl! Another question. What about making it rotate when pressing the arrow keys? :slight_smile:

EDIT, nevermind. I got it. :wink: Anyways, I’m going to figure out how to make it rotate when you press the right arrow! :smile:

EDIT2: How do I make it rotate? This is the script I’m using:

function Update () {
if (Input.GetKey(KeyCode.LeftArrow)) {
transform.Translate(Vector3.left * Time.deltaTime);
transform.Rotate(Vector3.left * Time.deltaTime);
}
}

Help is appreciated! :smile:

Both are pretty simple programatically, but I suggest a mathematical approach:

	var movSpeed=3.0;
	var rotSpeed=60.0;
	var mov = Input.GetAxis("Vertical") * Time.deltaTime * movSpeed;
	var rot = Input.GetAxis("Horizontal") * Time.deltaTime * rotSpeed;
	transform.Translate(Vector3(0,0,mov));
	transform.Rotate(Vector3(0,rot,0));

The Unity Script Reference will help you avoid small questions… :wink:

That one confuses me. Is there an easier way like the one Earl and I showed? I am a beginner, so, I wanna learn simply for now. :wink:

1 Like

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

var Speed : float = 5.0;
var RotationSpeed : float = 5.0;

function Update () {

if(Input.GetButton("Forward")){
transform.Translate(Vector3.forward * Time.deltaTime * Speed, Space.Self);
}

if(Input.GetButton("Reverse")){
transform.Translate(Vector3.forward * Time.deltaTime * -Speed, Space.Self);
}

if(Input.GetButton("Turn Left")){
transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
}

if(Input.GetButton("Turn Right")){
transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
}

}

Thanks for that. It works great. But, The object moves to fast. I modified the code to look like this, but it still is too fast. :face_with_spiral_eyes:

var Speed : float = 1.0;
var RotationSpeed : float = 5.0;

function Update () {

if(Input.GetKey(KeyCode.UpArrow)){
transform.Translate(Vector3.forward * Time.deltaTime * Speed, Space.Self);
}

if(Input.GetKey(KeyCode.DownArrow)){
transform.Translate(Vector3.forward * Time.deltaTime * -Speed, Space.Self);
}

if(Input.GetKey(KeyCode.LeftArrow)){
transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
}

if(Input.GetKey(KeyCode.RightArrow)){
transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
}

}

Thanks!

if it’s moving to fast for you go ahead and reduce the speed down to:

var Speed : float = 0.1;

Aalso check your objects scale it may be very small.

I figured it out. The variables were in the inspector. :wink: Thanks for all your guys help. :slight_smile: Last question, How do I make the object move at an increasing rate? Instead of 1 speed?

you could do something like a incresing speed variable

var CurrentSpeed : float = 0;
var Speed : float = 1.0;
var RotationSpeed : float = 5.0;

function Update () {

transform.Translate(Vector3.forward * Time.deltaTime * CurrentSpeed , Space.Self);

if(Input.GetKey(KeyCode.UpArrow)){
if(CurrentSpeed < Speed )
CurrentSpeed += Time.deltaTime;
}

if(Input.GetKey(KeyCode.DownArrow)){
if(CurrentSpeed > -Speed )
CurrentSpeed -= Time.deltaTime;
}

if(Input.GetKey(KeyCode.LeftArrow)){
transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
}

if(Input.GetKey(KeyCode.RightArrow)){
transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
}

}

And then in the next step you can decrease the speed automaticly :slight_smile:

var CurrentSpeed : float = 0;
var Speed : float = 1.0;
var RotationSpeed : float = 5.0;

function Update () {

transform.Translate(Vector3.forward * Time.deltaTime * CurrentSpeed , Space.Self);

if(CurrentSpeed < 0) {
CurrentSpeed += 0.25 * Time.deltaTime;
} else {
CurrentSpeed -= 0.25 * Time.deltaTime;
}

if(Input.GetKey(KeyCode.UpArrow)){
if(CurrentSpeed < Speed )
CurrentSpeed += Time.deltaTime;
}

if(Input.GetKey(KeyCode.DownArrow)){
if(CurrentSpeed > -Speed )
CurrentSpeed -= Time.deltaTime;
}

if(Input.GetKey(KeyCode.LeftArrow)){
transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
}

if(Input.GetKey(KeyCode.RightArrow)){
transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
}

}

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 ". :face_with_spiral_eyes: And, can you explain to me what all that code means? It’ll help me learn easier. :wink:

The only way to make it go faster is to increase the Speed var, or make a “Turbo” key that would add too the Speed var.

this is a non working example.

var turbo : int = 5;

if(Input.GetButton(“Turbo”){
Speed = Speed + Turbo;
}

Start with something like that. play around and see what you can come up with.

Also Checkout Unity Learn
it’s a good place to start.

Or you could just remove the Speed variable, but than your object can increase/decrease to any speed.
But if this is what you want :slight_smile:

var CurrentSpeed : float = 0;
var RotationSpeed : float = 5.0;

function Update () {

transform.Translate(Vector3.forward * Time.deltaTime * CurrentSpeed , Space.Self);

if(CurrentSpeed < 0) {
CurrentSpeed += 0.25 * Time.deltaTime;
} else {
CurrentSpeed -= 0.25 * Time.deltaTime;
}

if(Input.GetKey(KeyCode.UpArrow)){
CurrentSpeed += Time.deltaTime;
}

if(Input.GetKey(KeyCode.DownArrow)){
CurrentSpeed -= Time.deltaTime;
}

if(Input.GetKey(KeyCode.LeftArrow)){
transform.Rotate(Vector3.up * Time.deltaTime * -RotationSpeed, Space.Self);
}

if(Input.GetKey(KeyCode.RightArrow)){
transform.Rotate(Vector3.up * Time.deltaTime * RotationSpeed, Space.Self);
}

}

I like that code the best. But, without the Speed var, it goes too fast. :frowning:

Double post. I got it to be slower. :smile: 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 :slight_smile:

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.

Yes, i am trying to make a car move. I just don’t want to use the car tutorials because it’s too complicated for me to use. :slight_smile: