simple question... how to detect if you press w or s?

please help…this is my simple code:

var speed: float = 3.0;
function Update()

{
	if(Input.GetButton("Vertical"))
	{
              transform.Translate(Vector3(0,0,speed)* Time.deltaTime);
	}
}

if i press w the object will go forward but when i press s the object goes backward…
thx for helping:)

In terms of movement I would advise using Axes so instead of that you would do something like this:

public var speed : float = 5.0;
private var vertMovement : float  = 0.00;

function Update(){
    vertMovement = Input.GetAxis("Vertical");
    transform.Translate(Vector3.forward*(speed*vertMovement));
}

This will result in smooth movement because Axes use a value -1(pressing S) to 1(pressing W) or 0(pressing nothing) and would lerp between these values depending on your input settings. I mainly use the axes for vehicles using physics but it should work just as well with Translate.

To expand on the previous post:

GetButton is for mouse buttons. GetKey is for keyboard keys and uses KeyCode or a string to determine what key was pressed. However - explicitly binding keys in such a manner prevents users from customizing their inputs.

one thing… i don’t understand…
i can detect the keys that i’d press…(w,s)… when i’d press w it moves forward same as the s, it moves backward…
but when i pressed w again… it move backward…maybe i’d missed something here in my code…

var speed: float = 3.0;

var objMove: float = 0.00;



function Update()

{

	if(Input.GetButton("Vertical"))

	{

		objMove = Input.GetAxis("Vertical");

			if(objMove == 1){

				transform.Translate(Vector3(0,0,speed)* Time.deltaTime);

			}

			if(objMove == -1)

			{

				speed = -3.0;

				transform.Translate(Vector3(0,0,speed)* Time.deltaTime);

			}

	}

}

haha i got it… but the only problem is… when i press w the objMove(float) increases by float…and because Input.GetAxis(“Vertical”) only checks 1, 0 or -1… what if i press w without objMove going to -1 or 1… like 0.2315?

i’d used the if statement like this:

if(Input.GetButton("Vertical"))
	{
		objMove = Input.GetAxis("Vertical");
			if(objMove == 1){
				speed = 3.0;
				transform.Translate(Vector3(0,0,speed)* Time.deltaTime);
			}
			else
			{
				speed = -3.0;
				transform.Translate(Vector3(0,0,speed)* Time.deltaTime);
			}
	}

and if the objMove is less than 1 but greater than 0 when i press w after i’d press s…i goes backwards instead of forward…then goes forward afterwards because objMove goes 1. how could i fix it?

Well, you put the code I showed you inside of and if statement which you probably don’t need. It looks to me that you tried to combine Kelso and my own answer I would just avoid using the if statements altogether and multiply speed by objMove, otherwise I’m not quite sure what you mean…

uhh… even if i’d increase the speed… there’s something wrong when i press w after I’d press s…
there’s a slight backward movement while i’m pressing the w…when i’d check the value of the objMove…
it shows 0.203 something as I press w but it goes backwards for a bit then goes forward…
GetAxis only shows -1, 0 ,1 right? but that’s the problem, the slight backward

Hmm… I’m not sure what to tell you, mate. GetAxis returns -1 to 1 your 0.203 is merely your 1 (holding down W) lerping to 0 (pressing nothing) or whatever. I would advise on reading it up in the Script Reference.

This is everything that I have in my script

public var speed : float = 5.0;
private var vertMovement : float  = 0.00;

function Update(){
    vertMovement = Input.GetAxis("Vertical");
    transform.Translate(Vector3.forward*(speed*vertMovement));
}

This outcome produces a steady movement forward pressing the W key idle pressing nothing and backward movement when pressing S on the object it’s attached to, no anomalous oddities. Unless you’re trying to achieve something else other than forward/backwards movement

i got it… i got it…but i want it sideways too… left and right(a and d).
do I need to use if statement?

It works the same way but instead of using “Vertical” axis use “Horizontal” and change the translation direction to Vector3.right.