Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

How to make player movement

A topic by Satyam Mishra created Feb 17, 2021 Views: 470 Replies: 9
Viewing posts 1 to 4

how to make player movement in C# in unity with keyboard 

Which type. Do you want Rigidbody or Transform.

Transform

Deleted post

sorry but I am not in 2d

Here is how to make 3D movement with transform.

float h = Input.GetAxisRaw("Horizontal");
            float v = Input.GetAxisRaw("Vertical");
            transform.Translate(h * Time.deltaTime * speed, 0f, v * Time.deltaTime * speed);


Remember to create a public float for the speed variable. Like this.

public float speed = 5f;

You can change 5f to what ever you want. Just make sure to add f at the end. Example: 10f or 15f.

And you can still do physics.

I hope this helped.

Thanks

public float moveSpeed;

void Update{

float horiz = Input.GetAxis("Horizontal") * moveSpeed;

float vert = Input.GetAxis("Vertical") * moveSpeed;

transform.position += transform.foward * vert * Time.deltaTime;

transform.position += transform.right * horiz * Time.deltaTime;

}

For this script it's pretty simple and if you didn't mess with the Input Manager (Edit>Project Settings>Input Manager>Axes) Then it will grab the horizontal and vertical inputs which goes for both WASD and Arrow Keys and will even work for controller if you wanted that. 

Pog gamingnerd the guy seemed as he was struggling and i have no experience in 3d lol

Submitted

You can also use the new input system and instead of using axis, use a 2d vector