trouble with player controller please help-

I’m very new to scripting and this code was copied very carefully word for word from this youtube video

My character will jump just fine but she wont move left or right when I press the corresponding A or D keys. She will jump if I add a value next to the “moveSpeed” that controls the A and D key movement so I was thinking something was wrong with the moveSpeed component? I gave a value to the movespeed in the unity outside of monodevelop, as I was instructed to do by the video. So I really don’t know what I’ve done wrong and could really use some help! Thank you so much

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {

public float moveSpeed;
public float jumpHeight;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space))
{
GetComponent().velocity = new Vector2 (0, jumpHeight);
}
if (Input.GetKey (KeyCode.D))
{
GetComponent().velocity = new Vector2 (moveSpeed,0);
}
if (Input.GetKey (KeyCode.A))
{
GetComponent().velocity = new Vector2 (-moveSpeed,0);
}
}
}

So, I clicked your video link and it seems that you didn’t watch the rest of the video that explains how to make the movement + jump work a little nicer. :slight_smile: Just watch the rest of it :wink:

Please read this page for future posts: Using code tags properly - Unity Engine - Unity Discussions
It will insert code on the forums to look much nicer, like this:

if (Input.GetKey (KeyCode.A))
{
GetComponent<Rigidbody2D>().velocity = new Vector2 (-moveSpeed,0);
}

For example :slight_smile:

Thankyou so much for replying!

I did watch the whole video, but when he compiles his code after adding a script for the character to move horizontally, and I copy what he has done my character will not move horizontally. Although I had no problem getting my character to move on the y axis. I think you are saying I missed when he changes the y values in the horizontal movement code toward the end of the video so the character can move from side to side while in mid-air… but I have not added that into my code yet because I became stuck at this earlier part and I thought it would make more sense to figure out what I had done wrong so far before moving on!

(thanks again!)

Oh, okay… then I partially misunderstood your question. Okay, so in Unity, when you click on the game object and look on the right (for the inspector), there’s a script attached, and on the script you see the variable “moveSpeed” – is that where you added the value? Because if you added it there, that’s correct, and the rest of the script looks good… It should be working? :slight_smile:

Here, I made an extremely simple package to show/test… just copied your script in put a few blocks and ta-da :wink:
you can download & look at the package if you want, if yours still isn’t working for some reason.

3103791–234407–2dPlayerMove.unitypackage (6.28 KB)

Ah thanks so much!- the package you provided is working the way it should! I did edit the moveSpeed in the inspector so I don’t know why my situation was/is wonky, but I’ll keep messing around with it.

Okay. Well the package I uploaded is very small, so just compare it to yours, and maybe something will stand out :slight_smile:
Good luck :slight_smile: