how to add movement in unity 3d

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

public class PlayerMovement : MonoBehaviour
{
    //this is for first person movement
    public CharacterController controller;
    public float speed = 12f;
    Vector3 velocity;
    public float gravity = -9.81f;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    public Transform groundCheck;
    bool isGrounded;
    float jumpheight = 5f;
 
    // Update is called once per frame
    void Update()
    {
        //movement
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);

        //gravity
        velocity.y += gravity * Time.deltaTime;

        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        controller.Move(velocity * Time.deltaTime);

        if(isGrounded && velocity.y >= 0)
        {
            velocity.y = -2f;
        }

        //jumping
        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpheight * -2f * gravity);
        }

        //sprinting
        if(Input.GetKeyDown(KeyCode.LeftShift))
        {
            speed = 20f;
        }
        else
        {
            speed = 12f;
        }
    }
}

4.2
5
Kittyngwy 130 points

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

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

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


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

       ApplyInput(moveAxis);

       if(Input.GetButtonDown("Jump") && 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 == "Ground") {
            cubeIsOnTheGround = true;
        }
    }
}

4.2 (5 Votes)
0
4.2
5

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

public class PlayerMovement : MonoBehaviour
{
	public Transform cam;
	public CharacterController controller;
	public float speed = 6f;
	public float turnSmoothTime = 0.4f;
	public float jumpForce = 10f;
	float turnSmoothVelocity;
	public LayerMask groundMask;
	public float groundDistance = 0.4f;
	public Transform groundCheck;
	bool isGrounded = true;
	Vector3 velocity;
	public float gravityForce = -19.62f;
	float _slopeAngle;

	//Update is called once per frame
	void Update()
	{
		float horizontal = Input.GetAxisRaw("Horizontal");
		float vertical = Input.GetAxisRaw("Vertical");
		Vector3 direction = new Vector3(horizontal, 0f, vertical);

		if (direction.magnitude >= 0.1f)
		{
			float targetAngle = Mathf.Atan2(direction.x, direction.y) * Mathf.Rad2Deg + cam.eulerAngles.y;
			float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
			transform.rotation = Quaternion.Euler(0f, angle, 0f);

			Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
			controller.Move(moveDir.normalized * speed * Time.deltaTime);
		}

        //jumping
        if (isGrounded)
        {
			if (Input.GetKeyDown(KeyCode.Space))
			{
				velocity.y = Mathf.Sqrt(jumpForce * -2f * gravityForce);
			}
		}
	}

	void FixedUpdate()
	{
		if (isGrounded)
		{
			if (Input.GetKeyDown(KeyCode.LeftShift))
			{
				speed = 20f;
			}
			else if (Input.GetKeyUp(KeyCode.LeftShift))
			{
				speed = 6f;
			}
		}

		//Ground check
		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

		//gravity
		velocity.y += gravityForce * Time.deltaTime;

		isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
		controller.Move(velocity * Time.deltaTime);

		if (isGrounded && velocity.y < 0)
		{
			velocity.y = -2f;
		}
	}
}

4.2 (5 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
3d unity movement basic movement unity 3d unity movement example how to add movement to a gameobject how to add movement in unity add movement to unity 3d add movement to unity how to add movement in unity 3d how to create movement in unity with c# script to add movement to my player in unity unity movement guide how to add movement script in unity unity adding movement implement movement in unity 3d unity add movement to object make movement in unity unity simple player movement player movement in unity how to move player unity unity 3d movement system simple player movement in unity unity player movenet how to make a character move in unity unity player movement\ western movement unity c# how to make move in unity simple movement in unity how to make a basic movement for the player in unity vector3 movement unity how to use simple move in unity how to make quick movement script in Unity unity how to move player how to crete movement i unity move players in unity unity create mouvement how to make a simple player movement in unity 3d 2019 how to make a simple player movement in unity 3d how to make a moveable character in unity 2019 how to make a moveable character in unity unity player is moving unity how to move moving script unity 2d how toadd movement in unity basic movement system unity simple movement code unity How to implement movement in unity how to program movement in unity how to make a movement script in unity how to make player move unity among us movement code unity code for movement in unity unity add player movement how to make a basic movement script in unity how to make movment in unity movement code basic movemnet script unitry how to make a character move in unity c# move tuturial unity unity c# player movement script how to code movement in unity how to make player move in unity simple movement script unity unity movemnt unity simple player movement script making a moving character unity add movement unity unity movmeent how to make movement in unity in c# unity movement tutorial unity how to make movement movement of player in unity unity movement sample simple player movement script unity move player script unity unity charicter movementy how do you program movement in unity movements unity how to add movement to unity how to move player in unity unity easy movement how to create movement in unity 3d movement code unity 3d script for movement in 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# unity movment unity 3d movment how to make proper movement on unity movement script in unity how to code player movement in unity how to make movement unity how to do movement in unity C# player movment unity player movement unity 3d basic movement unity unity moveposition character movements movement in unity 3d how to make player movement in unity collision make player move unity how to make a player movement script in unity unity how to program movement how to move in unity how to move a player in unity movement in unity how to make movement of a player unity unity basic player movement simple player movement unity unity basic movement basic player movement unity unity 3d movement make unity ccharacter mobile movemevt unity player movement UNity simple movement player movement unity player movemtn unity www.how to make 3d movement in unity 2020 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