Lots of errors

Hey guys I’ve found this code on the internet to make a character move when you press a key and I keep getting lots of errors. I am a beginner (downloaded unity yesterday) and do not know anything about coding except a little python in school. here are the codes and errors:

Using System.Collections();
Using UnityEngine;
if (Input.GetKey(KeyCode.LeftArrow))
    {
        return;
    }
    Vector3 position = this.transform.position;
    position.x--;
    this.transform.position =  position;

The errors i get are:


P-S: it might be a bit weird because i tried adding things to correct the errors but they were already here.

Try something like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {
    // Update is called once per frame
    void Update () {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
        return;
        }
        Vector3 position = this.transform.position;
        position.x--;
        this.transform.position =  position;
    }
}

I haven’t tested it though! (And replace the PlayerController name whatever the filename is (without the extension)!)
And I suggest you to go through the programing paths at https://learn.unity.com/. Those tutorials are totally free and they are teaching you a bit of c# programming as well.

Thanks I had gave and taken another code that worked but this one is better!
I have another issue: I am trying to animate a character to have a turning animation instead of just walking backwards.
This is the code:

using UnityEngine;
{ }
public Animator animator;
    // Use this for initialization
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
{
    if (Input.GetKey(KeyCode.LeftArrow)) ; (Input.GetKeyDown(KeyCode.RightArrow));
        {
            animator.SetTrigger("turn 180 left");
        }
    }
and these are the errors i get![7624804--948481--2021-11-03 (2).png|1524x191](upload://zBSdPndMnPAielvaKl6P1e0KOB7.png)thanks very much and I promise ill go and do unity learn!

You may try this:

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public Animator animator;

    // Use this for initialization
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
        {
            animator.SetTrigger("turn 180 left");
        }
    }
}

But again I haven’t tested this … just tried to correct obvious problems with the code …
And another suggestion, if you allow me: setup Visual Studio or Visual Studio Code for editing, that is much more convenient.

Thanks!
PS ive started unity learn :slight_smile:

1 Like