hacklink al hack forum organik hit kayseri escort b-ok zeta library books z librarygrandpashabetgrandpashabetjojobet güncel girişjojobet 1019imajbetbetturkeySlot Oyunlarıankara escortlidoSoft2betdeneme bonusu veren sitelerjojobetdinamobet girişjojobetjojobetjojobetjojobet girişjojobet girişjojobet girişjojobet,jojobet giriş,jojobet güncel giriş,jojobet resmi girişzbahisparibahisbaşarıbetcasibompadişahbet girişstarzbet

Post Details Page

At Devhq.in, we provide comprehensive digital solutions for businesses. From software development and app creation to social media management and brand design, we help you build and grow your digital presence.

Simple Player movement in Unity for Beginner

I will tell you how can will make a player movement in unity easily.

Make a New Project in unity.

After making New project create a Ground.

I have create a Empty object name Player and inside that i have created my player that is Cube.

Now select player and Add Rigidbody Component for Real Physics and Character controller for control our player and adjust Height and radius According to the Player shape

Check the box of Is Kinematic in RigidBody

Now Create a New C# Script for Control player 📁PlayerMovement.cs

And Attach with Player Components like this you can drag and drop

Now this is Coding Time
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    // Variables 
    [SerializeField] private float moveSpeed;
    [SerializeField] private float walkSpeed = 10f;
    [SerializeField] private float runSpeed;

    // References 
    private Vector3 moveDirection;
    private CharacterController characterController;
    void Start()
    {
        // Asisign Character Controller components in Variable 
        characterController = GetComponent<CharacterController>(); 
    }
    void Update()
    {
        Move();
    }


    private void Move()
    {
        float moveX = Input.GetAxis("Horizontal"); 
        float moveZ= Input.GetAxis("Vertical");  // if  we press w then 1 and when press s then -1
        moveDirection = new Vector3(moveX, 0, moveZ);
        moveDirection = moveDirection * walkSpeed;
        
        characterController.Move(moveDirection*Time.deltaTime);
     }
}
C#

Now the player Basic movement is Completed

If your character is not on the ground then do this

Skin Width make Minimum

Now Make Running and Walking Script

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

public class PlayerMovement : MonoBehaviour
{
    // Variables 
    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private float walkSpeed = 5f;
    [SerializeField] private float runSpeed = 10f;

    // References 
    private Vector3 moveDirection;
    private CharacterController characterController;
    void Start()
    {
        // Asisign Character Controller components in Variable 
        characterController = GetComponent<CharacterController>(); 
    }
    void Update()
    {
        Move();
    }


    private void Move()
    {
        float moveX = Input.GetAxis("Horizontal"); 
        float moveZ= Input.GetAxis("Vertical");  // if  we press w then 1 and when press s then -1
        moveDirection = new Vector3(moveX, 0, moveZ);
        //  moveDirection = moveDirection * walkSpeed;

        if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
        {
            // Walk 
            Walk();
        }
        else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
        {
            // Run
            Run();
        }
        else if(moveDirection != Vector3.zero)
        {
            // Idle
            Idle();
        }
        moveDirection = moveDirection * moveSpeed;
        characterController.Move(moveDirection*Time.deltaTime);
     }

    void Idle()
    {

    }

    void Walk()
    {
        moveSpeed = walkSpeed;
    }

    void Run()
    {
        moveSpeed = runSpeed;
    }
}
C#

Make This type of Function for better understanding

Now make Jump and Check Player is Standing on The ground or Not
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    // Variables 
    private Vector3 moveDirection;
    private Vector3 velocity;

    [SerializeField] private float moveSpeed = 5f;
    [SerializeField] private float walkSpeed = 5f;
    [SerializeField] private float runSpeed = 10f;

    [SerializeField] private bool isGrounded;
    [SerializeField] private float groundCheckDistance = 1f;
    [SerializeField] private LayerMask groundMask;
    [SerializeField] private float gravity = -9.81f;
    [SerializeField] private float jumpHeight = 1f;
    // References 

    private CharacterController characterController;
    void Start()
    {
        // Asisign Character Controller components in Variable 
        characterController = GetComponent<CharacterController>(); 
    }
    void Update()
    {
        Move();
    }


    private void Move()
    {
        isGrounded = Physics.CheckSphere(transform.position, groundCheckDistance, groundMask);
        if (isGrounded && velocity.y<0) 
        {
            velocity.y = -2f;
        }
        float moveX = Input.GetAxis("Horizontal"); 
        float moveZ= Input.GetAxis("Vertical");  // if  we press w then 1 and when press s then -1
        moveDirection = new Vector3(moveX, 0, moveZ);
        //  moveDirection = moveDirection * walkSpeed;
        if (isGrounded) 
        { 
        if (moveDirection != Vector3.zero && !Input.GetKey(KeyCode.LeftShift))
        {
            // Walk 
            Walk();
        }
        else if(moveDirection != Vector3.zero && Input.GetKey(KeyCode.LeftShift))
        {
            // Run
            Run();
        }
        else if(moveDirection != Vector3.zero)
        {
            // Idle
            Idle();
        }
            moveDirection = moveDirection * moveSpeed;
            if(Input.GetKeyDown(KeyCode.Space))
            {
                Jump();
            }
        } 
        
        characterController.Move(moveDirection*Time.deltaTime);
        // apply gravity 
        velocity.y += gravity * Time.deltaTime;
        // Apply to the charachter 
        characterController.Move(velocity*Time.deltaTime);

     }

    void Idle()
    {

    }

    void Walk()
    {
        moveSpeed = walkSpeed;
    }

    void Run()
    {
        moveSpeed = runSpeed;
    }

    void Jump()
    {
        velocity.y = Mathf.Sqrt(jumpHeight *-2* gravity);

    }
}
C#

Note : Make sure You ground component contating ground layer .

Give Ground Mask to your ground layer and other thing now you can jump

Share now :

Facebook
Twitter
LinkedIn
Review Your Cart
0
Add Coupon Code
Subtotal