Hey!
I am trying to develop a sort of 3D Pong type game and im trying to get the movement squared away. I am just having trouble figuring out how to on the press of a button like “A” and “D” to communicate that i want to move negatively and positively along the X axis. I know that i need a speed and I have looked at the input class but I am just looking for some guidance. Thanks!
Well, you could do this:
transform.Translate(Input.GetAxis(“Horizontal”) * modifier, 0, 0);
thanks GargerathSunman but what exactly do you mean by “modifier” like i said im a bit new to this…
“modifier” is the generic variable that determines how much you move along the axis. For instance, if you wanted to move really really fast, modifier could be 10,000. However, if you wanted the movement to be variable, modifier could be set to Random.value each turn.
It’s just a modifier.
You also need to specify Time.deltaTime to avoid framerate dependence. Personally I’d use Vector3.right for the x axis which makes it more clear what’s going on (and is a little easier to modify). You’d also presumably want to limit movement so the paddle doesn’t go out of bounds.
var moveSpeed = 2.0;
var moveLimit = 5.0;
function Update () {
transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed);
transform.position.x = Mathf.Clamp(transform.position.x, -moveLimit, moveLimit);
}
–Eric
THANKS! that worked beautifully i really appreciate it.