unity player movement script

using UnityEngine;
using System.Collections;

// This script moves the character controller forward
// and sideways based on the arrow keys.
// It also jumps when pressing space.
// Make sure to attach a character controller to the same game object.
// It is recommended that you make only one call to Move or SimpleMove per frame.

public class ExampleClass : MonoBehaviour
{
    CharacterController characterController;

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

    private Vector3 moveDirection = Vector3.zero;

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

    void Update()
    {
        if (characterController.isGrounded)
        {
            // We are grounded, so recalculate
            // move direction directly from axes

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

            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
            }
        }

        // Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
        // when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
        // as an acceleration (ms^-2)
        moveDirection.y -= gravity * Time.deltaTime;

        // Move the controller
        characterController.Move(moveDirection * Time.deltaTime);
    }
}

4.1
10
TriSSSe 100 points

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

public class Example : MonoBehaviour
{
    private CharacterController controller;
    private Vector3 playerVelocity;
    private bool groundedPlayer;
    private float playerSpeed = 2.0f;
    private float jumpHeight = 1.0f;
    private float gravityValue = -9.81f;

    private void Start()
    {
        controller = gameObject.AddComponent&lt;CharacterController&gt;();
    }

    void Update()
    {
        groundedPlayer = controller.isGrounded;
        if (groundedPlayer &amp;&amp; playerVelocity.y &lt; 0)
        {
            playerVelocity.y = 0f;
        }

        Vector3 move = new Vector3(Input.GetAxis(&quot;Horizontal&quot;), 0, Input.GetAxis(&quot;Vertical&quot;));
        controller.Move(move * Time.deltaTime * playerSpeed);

        if (move != Vector3.zero)
        {
            gameObject.transform.forward = move;
        }

        // Changes the height position of the player..
        if (Input.GetButtonDown(&quot;Jump&quot;) &amp;&amp; groundedPlayer)
        {
            playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
        }

        playerVelocity.y += gravityValue * Time.deltaTime;
        controller.Move(playerVelocity * Time.deltaTime);
    }
}

4.1 (10 Votes)
0
3
4

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

public class PlayerMovement : MonoBehaviour
{
    private string moveInputAxis = &quot;Vertical&quot;;
    

    public float moveSpeed = 0.1f;
    public Rigidbody rb;
    public bool cubeIsOnTheGround = true;


    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent&lt;Rigidbody&gt;();
    }
    
    // Update is called once per frame
    void Update()
    {
       float moveAxis = Input.GetAxis(moveInputAxis); 

       ApplyInput(moveAxis);

       if(Input.GetButtonDown(&quot;Jump&quot;) &amp;&amp; cubeIsOnTheGround == true)
       {
           rb.AddForce(new Vector3(0, 7, 0), ForceMode.Impulse);
           cubeIsOnTheGround = false;
       } 

    private void ApplyInput(float moveInput)
    {
        Move(moveInput);
    }

    private void Move(float input)
    {
        transform.Translate(Vector3.forward * input * moveSpeed);
    }    

    private void OnCollisionEnter(Collision collision) {
        if(collision.gameObject.tag == &quot;Ground&quot;) {
            cubeIsOnTheGround = true;
        }
    }
}

3 (4 Votes)
0
3.88
8
BirdLaw 65 points

                                    //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&lt;CharacterController&gt;();
    }

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

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

        if (characterController.isGrounded)
        {

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

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

3.88 (8 Votes)
0
3.75
4
Neversaint 85 points

                                    using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] private float speed = 5.0f;

    private void Update()
    {
        var horizontal = Input.GetAxis(&quot;Horizontal&quot;);
        var vertical = Input.GetAxis(&quot;Vertical&quot;);
        transform.Translate(new Vector3(horizontal, 0, vertical) * (speed * Time.deltaTime));
    }
}

3.75 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
unity player movement guide unity movement example unity 2d movement new movement script unity unity3d how to make a character move 3d movement script in unity how to add movement to a gameobject 3d player movement in unity make a player movement unity how to move character to a position in unity? basic movement script c# script move character unity movement code c# play movement unity script c# unity player movement script unity c# 3d movement script how to add movement in unity add movement to unity 3d add movement to unity minecraft player movement unity how to make a movement script unity unity basic player movement script move character in unity player movement unity 3d c# unity character.move unity example movement script unity object movement script vector player movement unity basic player movement script for unity basic 3d movement script unity move character script unity how to move character unity c# unity movement script Movement scripting Unity how to add movement in unity 3d move a character in unity unity game movement 3d player movement script in Unity c# unity movement scripting movement script csharp unity movement script with movement how to move character using code in unity player movement script in unity move 3d character unity How to make a character move with unity 3d How to make a Unity character move simple movement script unity 3d how to make the character move in unity how to move character up in unity how to create movement in unity with c# basic movement c# unity best way to move character 3d player movement unity code Unity player movement s unity how to make a character move c# unity move character via code what is the best way to make a player movement script unity c# unity basic movement script How to make character move unity movement scripts in unity script to add movement to my player in unity unity movement guide how to make a character move in unity 3d unity3d script player movement unity 3d basic movement script how to make a character move in unity 3d c# move character unity how to add movement script in unity unity player movement 3d c# how to move the character unity 3d movement script unity c# move character unity 3d movement unity script C# basic movement script movement script unity movement script unity c3 unity script for player movement unity make character move unity movement explained unity player movement script download unity adding movement how to move character in unity 3d unity how to make character move unity game dev how to move character how to create player movement in unity implement movement in unity 3d how to code a movement script in unity unity 3d player movement script movement script c# unity unity player movementy unity code to make character move unity 3d make character move unity add movement to object make movement in unity movment code in unity moving c# code for unity player movement unity unity rig character rigidbody movement unity VR unity simple player movement unity 2d movement script character controller script unity character controller . move unity 3c movement movement in unity c# unity what is the best method to move character 3d character movement script player movement in unity unity3d player movement script unity character movement 3d how to make character move in unity unity what is character controller move player move script unity unity 3d movement controller unity 3d move player unity scripting basic controls move player C# character controller unity move how to code the best movement in c# simple movement c# unity unity free movement script how to move player unity player movement 3d movement unity3d code movement unity 3d code moving script unity basic player movement script unity unity 3d movement system character controller script unity simple player movement unity3d walk script unity plug in script character movement unity using plug in script character movement unity unity walk script 3d unity walk script player move command unity player move command uniti&igrave;y player move command unity&ugrave; make a player move in unity unity script movement character controller script for unity 3d simple player movement in unity unity player movenet movement controller unity 3d unity move player script how to make a character move in unity unity make a player move c++ 3D movement script move a player with code unity c# unity player movement\ how to make a movement script in unity 3d form.move unity western movement unity c# c# movement script 3d how to script player movement in unity unity basic 3d movement code for player c# unity unity simple movement script 3d download move player c# movement code how to make move in unity basic character movement unity c# simple movement in unity c# code movement 3D how to make player movement unity 3d player model movement unity 3d script controller script unity unity moving character unity character script player controller script unity how to make a basic movement for the player in unity unity character controller script vector3 movement unity 3d player controller script unity how to use simple move in unity unity character movement unity3d move player how to make player movement in unity 3d how to make quick movement script in Unity 3d movement script unity capsule unity movement script c# moving in unity player script unity c# character controller C Sharp Unity characther controller c# unity unity how to make a momvent script player movement code c# unity how to move player how to hgve my player move in unity unity how to make a basic movement script 3d Unity RTS movement custom script move in unity unity player scripts unity c# movement code simple player movement unity c# unity movement code 3d how to crete movement i unity pre made movement scpript unity unity c# 3d movement move players in unity 3d movement script character.move unity ho to make a player move in unity unity create mouvement movement script download unity player movement script 3d download how to make a simple player movement in unity 3d 2019 how to make a simple player movement in unity 3d unity player movement 3d script mnovement code unity player 3d movement unity unity move character with script unity make animation move character unity move script how to make a moveable character in unity 2019 how to make a moveable character in unity 3d unity movement script unity player is moving unity how to move 3d movement unity code unity movement player control unity script how to write a moving script unity mover script unity unity how to character movement moving script unity 2d making moveement scripts in unity script movement unity how to move a player model in unity unity player controller script C# unity player movement chacter movment script unity 3d movement in untiy 3D how to make move script unity UNITY CHARACHTER MOVE movement script unity 3d c# movement script for unity 3d how toadd movement in unity unity basic movement script basic 3d player movment scipt unity basic movement system unity simple movement code unity full movement script for unity unity character controller 3d script character movement unity How to make a movement script unity 3D C&pound; How to make a movement script unity 3D player movement c# unity unity movement function basic movement script How to implement movement in unity unity c# walk script unity move player 3d how to program movement in unity unity charecter movement script free unity player movement c# unity charecter movement script c# movement unity how to add a move player in unity movement script unity 2019 C# player movement script unity moving character in unity unity movement script. unity docs controller move 3d movement unity script unity a* moving c sharp movement script unity c sharp movement script unity playert mvemebt script how to make a movement script in unity player movement code in unity 3d player movement code in unity easy c# move script how to make player move unity free unity movement scrips character movement script Unity [player movement among us movement code unity movment script unity 2019 how to move a player unity movement unity scrip unity 3d simple movement move player charcater uniy unity documentation moving unity 3d move script code for movement in unity PlayerMovement script unity 3d character movement unity 3d character movement unity 3f unity add player movement player movement unity script movement controller unity documentation C# script move 3d unity documentation for moving sample move script unity how to make a basic movement script in unity unity3d movement unity how to move a player unity player movement 3d script c# how to make movment in unity movement code basic movemnet script unitry movement script unity 3d unity player movement code in c# player movement unity3d example movement unity how to make a character move in unity c# c# unity walking script 3d function move unity move tuturial unity how to move player in unity 3d Make a character movable in unity unity movement code player movement script for unity unity player movement script 3d reference player movement controller script unity 3d unity c# player movement script how to code movement in unity 3d how to code movement in unity movement coding unity 3d c# how to make player move in unity unity how to make a player move unity c# movement script simple movement script unity c# movement script unity basic character controller unity unity movemnt player movement 3d unity unity simple player movement script making a moving character unity add movement unity how to move a character in unity 3d how to move a character in unity how to move unity script unity 3d player movementscript unity player movementscript reposition player in unity movement unity3d script movement unity3d scripts unity movmeent move unity how to make movement in unity in c# unity 3d player move script unity character controller script 3d 3d character controller unity script unity moving a player 2018 unity movement script move in unity unity 3d player controller script moving player unity move a play in unity unity movement tutorial unity how to make a character move how to move the character in unity unity 3d player movement script c# unity how to make movement playermovement type unity unity script player movement unity c# player movement movement of player in unity c# movemenr c# movement' unity character movement 3d scripts free download unity movement sample Character movement code for c# unity script moving unity character move script C# code for character movement unity 3d movemnet player c# unity unity move unity moveinput scrupt how do i make a player move unity how to move in unity 3d simple player movement script unity move player script unity unity charicter movementy 3d character controller script unity how to make a palyer move in unity unity 3d character controller script basic 3d movement unity how do you program movement in unity controller movement in unity simple C# movement script movements unity unity on move function unity on m ove how do you make a carather move in unity How to muve in unity 3d moving in unity 3d unity 3d movement code how to add movement to unity unity script formovement unity script for movement 3d player movment unity how to make a moving character in unity script unity 3d movement code to move in unity unity player movement script c# 3d c# simple movement unity character code unity movement script pc controller movement script unity 3D unity (!moving) unity simple move script how to move player in unity 3d player movement c# unity easy movement how to create movement in unity 3d script unity player movement movement code unity 3d C# movemoent script movement code unity download movement code unity script for movement in unity how to make it look like player is moving unity 3d movment c# unity unity player movement sdcript c# unity how to move simple character movement unity physics walk unity script how to add player movement in unity how to make a player move in unity unity plauer movement script unity player movement script c# movment unity player movement script 3d 2020 c# unity movment Unity player movement script C# c# movement 3d c# 3d movement c# 3d movements c# movement unity documentation c#movement movements control unity unity documentation c# wsad movement unity documentation c# movement unity 3d movment character movement script unity how to move character in unity unity character movement script how does .move work in unity how to make proper movement on unity unity script move player move script unity movement script in unity unity move method how to move a palyer in unity unity Move unity simple movement script unity moving in script unity player movement script 3d motion player unity unity player script 3d character movement script unity how to code player movement in unity how to change movement in unity safely move scripts in unity 3d movement script unity simple unity movement script 3d unity movement unity code movement how to make a basic move and looking script in unity 3d how to make movement unity input control movement script unity 3d player movement unity 3d motion script c# unity player movement script unity c# how to do movement in unity C# unity movement controller movement script 3d unity player script unity movement scipt for character in unity C# player movment unity player movement unity 3d unity player movement code how to make the player move in unity basic movement unity unity moveposition character movements unity writing movement scripts how to make your character move in unity 3d unity player movement movement in unity 3d how to code a movment c# Basic movement script in unity 3d unity character controller move full movement script for unity 3d basic movement script unity 3d Player movement c# script how to make player movement in unity unity basic player movement 3d collision make player move unity character move script for unity unity 3d player movemnt controller sciprt unity 2019 simple player movement script 3d how to make a player movement in unity how to make a player movement script in unity how to make a movement script in C# 3d movement in unity full movement in unity unity how to program movement 3d character movement unity unity 3d character movement script unity 3d character movement 3d player movement script unity player movment script unity basic character movement unity c# player movement script 3d player movement unity unity 3d movescript c# player movement script c# Unity movement script 3D unity move command movement 3d unity how to move in unity how to move a player in unity Move() unity unity 3d movement script c# how to move character in script unity unity move player move player unity how to make player move in unity 3d movement script unity movement in unity unity move character how to make movement of a player unity movement c# unity basic player movement simple player movement unity c# script movement C# movement script 3d playermovement unity unity C# movement C# how to move player unity player movement 3d movement in c# simple 3d movement script 3d movement unity unity 3d player movement unity basic movement basic unity movement script unity&igrave; movement 3d script unity&igrave; movement script movement unity 3d basic player movement unity basic movement script unity movement script unity movement 3d unity c# player movement script 3d unity 3d movement make unity ccharacter mobile movemevt unity player movement c# walking script c# moving character unity c# moving caracter UNity simple movement player movement unity unity3d player movement unity movement 3d script player movemtn unity unity movement script unity 3d movement script how to move in unity c# unity default movement script walking script unity motion script unity www.how to make 3d movement in unity 2020 player movement unity 3d script player movement script unity 3d script movement unity 3d movement script c# 2d player movement script in unity unity 2D camera follow c# character controller adding colliders in unity player movement script unity movement unity unity movement how to do movement in unity how to make movement in unity how to add movement in unity
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source