Rookie Problem: transform.translate

Hello,

I’m having some trouble with the code below. I’m trying to set up the vertical movement of a gameObject. The problem is, when I press ‘S’, the gameobject moves once, then stops. I thought that putting the input check inside the update loop would have it called once per frame, thereby creating continuous movement. Any help would be appreciated!

using UnityEngine;
using System.Collections;

public class PlayerPaddle : MonoBehaviour 
{
	public int speed; 

	// Use this for initialization
	void Start () 
	{
		speed = 100;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Input.GetKeyDown(KeyCode.S))
		{
			transform.Translate(0, -speed * Time.deltaTime, 0);
		}	
	}
}

You’re using GetKeyDown, which “Returns true during the frame the user starts pressing down the key identified by name.”

I would instead do this:

void Update ()
{
    transform.Translate(0, Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0);
}

This will use the Horizontal axis to apply movement both upwards and downwards (see Project Settings > Input). The Horizontal Axis automatically uses W for up and S for down (as well as the arrow keys).

You need to change this line:

if(Input.GetKeyDown(KeyCode.S))

to:

if(Input.GetKey(KeyCode.S))