Unity Character Controller Move Jump Gravity Velocitey Script - codebugfree

 We are discuss the chracter controller of the player in the unity . First we describe the  Defination of the character controller. Character controller is the one of the types of the unity component. It is use to move the control the character in the game. It have both advantage and disadvantage. 

It is more difficult with beginner game developer because they character controller have no own gravity and velocity. In character controller programmer write own the gravity velocity. Ther is no built in function of the gravity or other physics.

Unity Character Controller Move Jump Gravity Velocitey Script

Character controller isgood for the move the player because the programmer full controll in player movement means programmer have full control or edit in the physics. You can also known as the programmer write own physics system in the unity character controller component like jump move gravity velocity. Also there is own system to detect the colider in pcharacter controller.

Unity Character Controller Script



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

public class PlayerMovement : MonoBehaviour
{
    public CharacterController characterControlle;
    public float speed;

    [HideInInspector]
    public Vector3 move;
    public float gravity = -10;
    public Vector3 velocitey;
    public float jumpForce = 8;
    public Transform groundCheck; // Creaty a player children empty game object and set is down to trac the player is ground or not.
    public LayerMask groundLayer; // ad a layer in ground as a random name.
    public bool isground;
    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Horizontal");  // input system
        float z = Input.GetAxis("Vertical"); // input system
        move = transform.right * x + transform.forward * z;
        characterControlle.Move(move * speed * Time.deltaTime);
        isground = Physics.CheckSphere(groundCheck.position, 0.3f, groundLayer); //check the player in ground or not
        Debug.Log(isground);
        if (isground (Double and Sing) velocitey.y < 0)// NOTE: here Double and Sing is the double ampersand sing.
            velocitey.y = -1.0f;
        if (isground)
        {
            if (Input.GetButtonDown("Jump"))
            {
                jump();
            }
        }
        else
        {
            velocitey.y += gravity * Time.deltaTime;
        }
            characterControlle.Move(velocitey * Time.deltaTime);
        
    }
    private void jump()
    {
        velocitey.y = jumpForce;
        /// to climd the stair with programming not a increae the value of the slope limit in the player controller
        //velocitey.y = Mathf.Sqrt(jumpForce * 2 * -gravity);
    }
    
}

Her we is use the old input system as a Axies system. In jump method there is two types of the jump we recommand the first. Read this code and learn the character controller system.

Post a Comment

0 Comments