Skip to content

Instantly share code, notes, and snippets.

@robertcedwards
Last active February 25, 2024 22:27
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
}
}
@ChaboCode
Copy link

what does DeltaTiem stands for?

@Alabouh
Copy link

Alabouh commented Aug 14, 2020

@WotsitGamerYT

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.up*speed* Time.deltaTime;
	}forward;
	if (Input.GetKey(KeyCode.DownArrow)){
		transform.position += Vector3.down *speed * Time.deltaTime;
	}
}

it says: (1,6): error CS0116: A namespace cannot directly contain members such as fields or methods

@DrApplemango
Copy link

i love this

@AJ0541
Copy link

AJ0541 commented Oct 2, 2020

@WotsitGamerYT

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.up*speed* Time.deltaTime;
	}forward;
	if (Input.GetKey(KeyCode.DownArrow)){
		transform.position += Vector3.down *speed * Time.deltaTime;
	}
}

it says: (1,6): error CS0116: A namespace cannot directly contain members such as fields or methods

	private float speed = 4.0f;

public GameObject character;
// Update is called once per frame
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;
	}
} 

use this as movement script
then make another script for jump and copy paste this
rivate float speed = 10.0f;
// Use this for initialization
void Start () {

}
public GameObject character;
// Update is called once per frame
void Update () {
			if (Input.GetKey(KeyCode.Space)){
		transform.position += Vector3.up* speed * Time.deltaTime;
	}
}

@TeBidz
Copy link

TeBidz commented Oct 20, 2020

im still cant use that too

@NotZakariya
Copy link

when I press play my character falls through the floor
You need to add a box collider to the floor

@NotZakariya
Copy link

how do you program controller buttons

@AlbinoTomato301
Copy link

when I press play my character falls through the floor

add a collider to your player and the object you want to stand on

@Xulum12
Copy link

Xulum12 commented Dec 20, 2020

if i paste out it don't works! it has a problem with the { but the next page ???? please help me! i don't find a perfect code. just i want to controll the camera with the w a s d or the arrows

@Xulum12
Copy link

Xulum12 commented Dec 20, 2020

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

@The-Maize
Copy link

what does DeltaTiem stands for?
Uses a real world time per init scale instead of a frame by frame speed.

@EibeZukunft83
Copy link

for Xulum12 if you still need the answer. I got the problem too but it was easy to solve. i think, im not sure if its same by you, yo uneed to
make a "{" after "public class (your script name) : MonoBehaviour" and at the end . For me it works.

@ZeNik0418
Copy link

what do i change so i use both WASD and ARROWKEYS for control

@The-Maize
Copy link

what do i change so i use both WASD and ARROWKEYS for control

Just add a second set of if statements with the same parameters. For example:

Void moveFunction()
{
if (Input.GetKey(KeyCode.W))
transform.Translate(0,0,1);

if (Input.GetKey(KeyCode.insert new 2nd key code here))
transform.Translate(0,0,1);
}

@cryskram
Copy link

cryskram commented Apr 7, 2021

what does DeltaTiem stands for?

Basically, it is the time it took to run the previous frame of instance

@touqan
Copy link

touqan commented Apr 24, 2021

Add a cube into your game. Add a Rigidbody component to the cube. Add a Movement Script to the cube.
Paste the following.

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

public class Movement : MonoBehaviour
{
public float forwardsForce = 40f;
public float sidewaysForce = 40f;

public Rigidbody rb;

 void Update() 
{
    MovePlayer();
}

void MovePlayer() 
{
    if (Input.GetKey(KeyCode.UpArrow))
    {
        rb.AddForce(0, 0, forwardsForce * Time.deltaTime, ForceMode.VelocityChange);
    }
    if (Input.GetKey(KeyCode.DownArrow))
    {
        rb.AddForce(0, 0, -forwardsForce * Time.deltaTime, ForceMode.VelocityChange);
    }
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }
    if (Input.GetKey(KeyCode.RightArrow))
    {
        rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }
}

}

Ctrl+ s to save go back into unity refrence the rigidbody.
And it should work.

@touqan
Copy link

touqan commented Apr 24, 2021

Oh and when you say for example you have a variable called moveSpeed that is = 10 if you say moveSpeed * Time.deltaTime basically your making moveSpeed frame rate independent.
What I mean is if you have a computer that runs 100 fps and a computer that runs 20 fps
moveSpeed would be 1 on the 100 fps computer. And it would be 5 on the 20 fps computer so it basically evens it out independent of your frame rate.

@kamadotanziro
Copy link

how to use mouse to look up,left/right,down

@Silent-Whisper
Copy link

Silent-Whisper commented Sep 12, 2021

Add a cube into your game. Add a Rigidbody component to the cube. Add a Movement Script to the cube.
Paste the following.

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

public class Movement : MonoBehaviour
{
public float forwardsForce = 40f;
public float sidewaysForce = 40f;

public Rigidbody rb;

 void Update() 
{
    MovePlayer();
}

void MovePlayer() 
{
    if (Input.GetKey(KeyCode.UpArrow))
    {
        rb.AddForce(0, 0, forwardsForce * Time.deltaTime, ForceMode.VelocityChange);
    }
    if (Input.GetKey(KeyCode.DownArrow))
    {
        rb.AddForce(0, 0, -forwardsForce * Time.deltaTime, ForceMode.VelocityChange);
    }
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }
    if (Input.GetKey(KeyCode.RightArrow))
    {
        rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }
}

}

Ctrl+ s to save go back into unity refrence the rigidbody.
And it should work.

### that didn't work for me so i modified it a bit:

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

public class Movement : MonoBehaviour
{
public float forwardsForce = 40f;
public float sidewaysForce = 40f;
public Vector3 up;
public Vector3 down;
public float jumpForce = 4f;

public Rigidbody rb;

void Start()
{
    up = new Vector3(0.0f, 2f, 0.0f);
    down = new Vector3(0.0f, -2f, 0.0f);
}
void Update()
{
    MovePlayer();
}

void MovePlayer()
{
    if (Input.GetKey(KeyCode.W))
    {
        rb.AddForce(up * jumpForce, ForceMode.Impulse);
    }
    if (Input.GetKey(KeyCode.S))
    {
        rb.AddForce(down * jumpForce, ForceMode.Impulse);
    }
    if (Input.GetKey(KeyCode.A))
    {
        rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }
    if (Input.GetKey(KeyCode.D))
    {
        rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    }
}

}

follow the same instructions.
Hope i helped someone!

@Pandaman73
Copy link

None of these work for me :/

@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

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