- Home /
need help with simple c# movement script
Hi, I don't understand what's wrong with this script, this has happened with several scripts I've tried. Basically it's all in red, most of the errors are from functions apparently not being in the current context etc. Keywords aren't working and are coming up with question marks in the debug panel. Here's the code:
using UnityEngine;
using System.Collections;
public class playerControl : MonoBehaviour
{
public float speed = 5f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Movement ();
}
void Movement()
{
if (Input.GetKey (KeyCode.D))
{
transform.Translate (Vector2.right * speed * Time.deltaTime);
}
else if (Input.GetKey (KeyCode.A))
{
transform.Translate (Vector2.left * speed * Time.deltaTime);
}
}
}
Any help would be majorly appreciated. If it is a simple mistake/typo etc, could you please also explain what I've screwed up and why it has screwed it up? I would love to understand because this is Uber annoying : /
Thanks in advance :)
Answer by Invertex · Feb 23, 2014 at 03:56 AM
Vector2.left does not exist, if you need the opposite of Vector2.right, just use -Vector2.right.
Thank you sooo much :D Helpful and to the point. I posted the same on stack overflow and got nothing but attitude : /
Hehe it's understandable there, they consider themselves a little bit "above" such common problems.
For future reference, if $$anonymous$$onodevelop is showing red, hover over the piece of code and it will tell you what's wrong. In this case, it tells you that "left" does not exist. :)
Your answer

Follow this Question
Related Questions
Enter Trigger, display Text, then delete object 1 Answer
NullReferenceException 3 Answers
Disable function OnTriggerExit? 2 Answers
Could not preload global game manager #0 3 Answers
error with learning script 2 Answers