Unusual error regarding movement in a top down 2d sidescroller

im trying to get my character to move. my code is (in C#):

using UnityEngine; using System.Collections;

public class PlayerMovement : MonoBehaviour {

public float speed;
 
void Update () {
 
    if(Input.GetKey [KeyCode.D]) {
 
        transform.Translate [Vector2.right * speed];
 
}
}

}

The error I get is error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement on the line with the transform code

What is my problem? thank you in advance

You’re not using function calls correctly - they have parentheses, not square braces.

The syntax is either:

if(Input.GetKey(KeyCode.D))

or

if(Input.GetKey("d"))

But if you want to make it support more than one key easily, you could get horizontal movement instead:

float h = Input.GetAxis("Horizontal") * speed;
float v = Input.GetAxis("Vertical") * speed;
transform.Translate(new Vector2(h, v));

This responds to A, D, left and right arrow for horizontal movement, W, S, up and down for vertical movement. All in two calls :slight_smile: