How to Make a Player Movement Script in Unity?

A player movement script is a script that tells the rendering engine of your Unity game, how to move your player. Movement on its own sounds pretty simple, but there are actually a lot of ways you can move your character around in a scene. 

C# player movement script unity

By Impossible IbexImpossible Ibex on Nov 12, 2020
//make sure to add a CharacterController to the thing that you want to move
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    CharacterController characterController;

    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public float speed = 9.0f;

    private Vector3 moveDirection = Vector3.zero;

    private void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        var horizontal = Input.GetAxis("Horizontal");
        var vertical = Input.GetAxis("Vertical");

        transform.Translate(new Vector3(horizontal, 0, vertical) * (speed * Time.deltaTime));

        if (characterController.isGrounded)
        {

            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
            moveDirection *= speed;

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }
        moveDirection.y -= gravity * Time.deltaTime;
        characterController.Move(moveDirection * Time.deltaTime);
    }
}

Add Comment

4

movement script c#

By Upset UnicornUpset Unicorn on Sep 20, 2020
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.up*speed* Time.deltaTime;
	}forward;
	if (Input.GetKey(KeyCode.DownArrow)){
		transform.position += Vector3.down *speed * Time.deltaTime;
	}
}

Add Comment

1

If you want to only check and see if your character is in the right place while they run through your level, they can use fixed step or transition movement

C# answers related to "C# player movement script unity"

View All C# queries

C# queries related to "C# player movement script unity"

C# player movement script unity Player movement with animation unity unity movement script 3d unity character movement script premade movement script c# unity smooth movement lerp unity normalize movement how to make an object face the movement direction in unity how to make movement in unity in c# object escape player unity player ToJson unity unity rotate player based on terrain player not following slide object unity 2d how to make a fps player in unity enemy look at player unity 2d C# velocity movement C sharp character movement #movement speed c good physics based movement C# movement instantiate a player in photon custom player spawner mirror navmesh follow player how do I attach a player with a navMeshAgent unity float from another script unity change tmp text from script unity set particle properties through script how do i limit the amount of prefabs in unity using c# script unity can't put tmpro in script how to make a follow script in unity unity set sprite image from script character control script unity How to make unity script editor open in visual studio not in Note pad how to access the dictionary from another script in unity unity public script gravity script unity how to change text to bold through script unity unity unfreeze position in script unity script wait unity die script unity move script if else how to reference a static variable from another script in unity unity script template folder HOW TO SET TAG IN SCRIPT UNITY unity how to get a script that is in Editor folder bounce script unity enemy turret one direction ahooting script unity 2d unity first person controller script unity remove component in script get int from another script unity unity teleport script 3D mouse click unity raycast unity freeze axis in script what function is called just before the a script is ended c# how to assign 2d physics material through script reference variable from another script "winforms" c# can't add an editor script How to execute a script after the c# function executed isGrounded script for copy google script get time google script get font color how to lock and hide a cursor unity reload scene unity header in inspector unity unity how to convert mouse screen position to world position how to detect a mouse click in unity unity mirror get ip address unity textmesh pro how to just aet z rotation on transform unity unity how to get y value unity mouse position to world unity c# set gameobject active Time delay C# unity unity check collider layer unity log unity print to console unity to string delete in unity fps camera unity unity how to set gameobjkect enabled dropdown text mesh pro unity unity mouse scroll wheel axis how to convert int to string unity c# object spin unity round to float unity require component unity Unity if or how to disable and enable rigidbody unity unity change text color unity set object scale Unity C# instantiate prefab unity transformer double en float How to create a list in csharp unity debug.log unity unity destroy all objects with tag load scene unity unity lerp position Debug unity unity instantiate prefab unity failed to load window layout unity c# transform position unity c# check how many of an object exists unity c# change image source destroy all objects under parent unity c# how to change the color of an object in unity c# rgb unity lerp toggle unity c# unity instantiate prefab as child how to change scenes in unity convert array to list Unity C# how to clamp a velocity in unity c# index out of bound unity c# press key run code unity c# unity c# static monobehaviour unity c# write line unity c# method after delay translate gameobject unity c# Disable Debug.log Unity load material unity c# how to add a variable in unity c# unity c# sin get shader unity c# unity c# find object position in array collision detector unity c# 2d how to make float in unity c# unity c# flip sprite unity read text file play sound on collision unity c# unity cancel invokerepeating unity create empty gameobject in code unity restore deleted files moving camera with touch screen unity random position unity 2d how to store some variables on the device in unity unity get refresh rate quadratic aiming unity unity ar scale for what is percent sign in unity c# how to download something form the web unity unity state machine behaviour for ai hwo to check if something is in a sphere in unity unity animator trigger stuck unity animation missing gameobject replace unity 2d enemy field of view character stay in ground unity 3d unity check if animator has parameter simple enemy spawning Unity how to save data on cellphone unity android parse float not working unity change z value unity unity stop object from rotating unity set parent canvas how to set minvalue of slider in unity compass direction mobile unity unity var not minus unity c# 10 random numbers unity unity rename populated list varibale true false when key pressed in c sharp unity unity android app black screen unity c# public all codes how to remove a parten transform unity unity check if right keyboard button is down unity smooth rotation 2sd layermask in unity unity get sign of number Unity make a homing object gaussian blur unity sprite 2D how to make % posibility to spawn an object C# in unity On add component unity unity c# jump how to do a sculpt in unity how to add colider in obj in unity 2020 destroy ui element unity unity predicts rigidbody position in x seconds clock in unity change character velocity unity unity check if current scene is being unloaded unity clear array unity insert variable into string buttons not working canvas group unity unity black screen unity find out sdk version in code Unity how to put IEnumerator in update and loop once with yeild return new waitforseconds refrerencing tags unity unity Couldn't acquire device ID for device name Built-in Microphone unity 2d swap out background image unity text change percentage how to import camera pos in unity unity slider decimal 0.01 unity buoncy changing change true to false unity unity spawn enemy waves how to play a random sound at the position that you want in unity using == is inefficient unity How to make an enemy unity unity dynamically set hinge joint spring target position unity rotate vector around point

Browse Other Code Languages

CodeProZone