Skip to content

Instantly share code, notes, and snippets.

@robertcedwards
Last active April 26, 2025 07:32
Show Gist options
  • Save robertcedwards/dbaa2955a6e6e13f06df to your computer and use it in GitHub Desktop.
Save robertcedwards/dbaa2955a6e6e13f06df to your computer and use it in GitHub Desktop.
Movement Script in C# for Unity
private float speed = 2.0f;
public GameObject character;
void Update () {
if (Input.GetKey(KeyCode.RightArrow)){
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow)){
transform.position += Vector3.left* speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow)){
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow)){
transform.position += Vector3.back* speed * Time.deltaTime;
}
}
@LordOfCheese1
Copy link

LordOfCheese1 commented Nov 19, 2021

Alright, if anyone is still here, apply a rigidbody and collider to whatever object your player is, apply a script called "PlayerMovement", and copy this code:

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

public class PlayerMovement : MonoBehaviour
{

public float speed = 2f;
// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if(Input.GetKey(KeyCode.W))
    {
        transform.position += Vector3.forward * speed * Time.deltaTime;
    }

     if(Input.GetKey(KeyCode.S))
    {
        transform.position += Vector3.back * speed * Time.deltaTime;
    }

     if(Input.GetKey(KeyCode.A))
    {
        transform.position += Vector3.left * speed * Time.deltaTime;
    }

     if(Input.GetKey(KeyCode.D))
    {
        transform.position += Vector3.right * speed * Time.deltaTime;
    }
}

}

It should work perfectly fine.

@ZEROOO12
Copy link

It's good but very slow

@Echo413
Copy link

Echo413 commented Jul 10, 2022

when I press play my character falls through the floor

Add Rigid body

@Echo413
Copy link

Echo413 commented Jul 10, 2022

what does DeltaTiem stands for?

the time that takes to load another frame

@Echo413
Copy link

Echo413 commented Jul 10, 2022

It's good but very slow

you can change the speed

@sprentthecat
Copy link

Alright, if anyone is still here, apply a rigidbody and collider to whatever object your player is, apply a script called "PlayerMovement", and copy this code:

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

public class PlayerMovement : MonoBehaviour {

public float speed = 2f;
// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    if(Input.GetKey(KeyCode.W))
    {
        transform.position += Vector3.forward * speed * Time.deltaTime;
    }

     if(Input.GetKey(KeyCode.S))
    {
        transform.position += Vector3.back * speed * Time.deltaTime;
    }

     if(Input.GetKey(KeyCode.A))
    {
        transform.position += Vector3.left * speed * Time.deltaTime;
    }

     if(Input.GetKey(KeyCode.D))
    {
        transform.position += Vector3.right * speed * Time.deltaTime;
    }
}

}

It should work perfectly fine.

it doesn't work,instead it does this

help.mp4

@Knightriderp
Copy link

It's good but very slow

you can change the speed

HOWWW

@worldofmemes291
Copy link

Thanks it worked I just had to remove the Rigidbody so it doesn't fly all over the terrain

@keremsngn61
Copy link

THANKS !!
It did, but it worked very slowly. I'll try another method. Thanks anyway! :)

@XeroPlayer
Copy link

making rigidbody a class in unity doesn't work for me. it gives me an error

@Joe-component
Copy link

the "vector3" in my visual studio it's not gteen, and it not works. please help!

hover on it and there will be a drop which is showing "using:UnityEngine" or something start with "using" tap on it but make sure you are using the lastest version of the unity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment