Player wont move

im having trouble moving my character it wont move any way at all the private float shows up in my inspector and i changed it to 5 and it still wont move here is the code im using please help using im also watching this video #1.0 Unity RPG Tutorial - Player movement - YouTube to learn to code
System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

[SerializeField]
private float speed;

private Vector2 direction;

// Use this for intialization
void Start()
{

}

// Update is called once per frame
void Update()
{
    GetInput();
    Move();
}

public void Move()
{
    transform.Translate(direction * speed * Time.deltaTime);
}

private void GetInput()
{
    direction = Vector2.zero;

    if (Input.GetKey(KeyCode.W))
    {
        direction += Vector2.up;
    }
    if (Input.GetKey(KeyCode.A))
    {
        direction += Vector2.left;
    }
    if (Input.GetKey(KeyCode.S))
    {
        direction += Vector2.down;
    }
    if (Input.GetKey(KeyCode.D))
    {
        direction += Vector2.right;
    }
}

}

I could be wrong but transform.translate takes a Vector3 instead of a Vector 2. Try:

transform.translate(new Vector3(direction.x, direction.y, 0) * speed * Time.deltaTime);

I tried running your script and it worked perfectly :slight_smile:

Perhaps you need to set your Speed value in the inspector like so:

Also, in reference to @Optiknights comment: Good thought, he can use a Vector2 so long as he is doing a side scroll and not a top down. If this is a top-down controller he would want to use a Vector3 and change the X and Z axis instead of the X and Y with the Vector 2.

@Ximithie this should work just fine so long as you have it attached to your player and declare a value in the inspector.

As a suggestion for someone learning to code, its also worth thinking about how you can debug this. Start by printing messages everywhere to see what is going on. Is direction being set to a non-zero vector? What is your position before translate is called. What about after? Is it moving, and perhaps, your scale is too large?

By the looks of it, this code should work. I would double check that your scene is set up correctly.