Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

CharacterController

using UnityEngine;

[RequireComponent(typeof(CharacterController))]

public class SC_CharacterController : MonoBehaviour
{
    public float speed = 7.5f;
    public float jumpSpeed = 8.0f;
    public float gravity = 20.0f;
    public Camera playerCamera;
    public float lookSpeed = 2.0f;
    public float lookXLimit = 45.0f;

    CharacterController characterController;
    Vector3 moveDirection = Vector3.zero;
    Vector2 rotation = Vector2.zero;

    [HideInInspector]
    public bool canMove = true;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
        rotation.y = transform.eulerAngles.y;
    }

    void Update()
    {
        if (characterController.isGrounded)
        {
            // We are grounded, so recalculate move direction based on axes
            Vector3 forward = transform.TransformDirection(Vector3.forward);
            Vector3 right = transform.TransformDirection(Vector3.right);
            float curSpeedX = canMove ? speed * Input.GetAxis("Vertical") : 0;
            float curSpeedY = canMove ? speed * Input.GetAxis("Horizontal") : 0;
            moveDirection = (forward * curSpeedX) + (right * curSpeedY);

            if (Input.GetButton("Jump") && canMove)
            {
                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);

        // Player and Camera rotation
        if (canMove)
        {
            rotation.y += Input.GetAxis("Mouse X") * lookSpeed;
            rotation.x += -Input.GetAxis("Mouse Y") * lookSpeed;
            rotation.x = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit);
            playerCamera.transform.localRotation = Quaternion.Euler(rotation.x, 0, 0);
            transform.eulerAngles = new Vector2(0, rotation.y);
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to split string into a list ignoring number of spaces c# 
Csharp :: permutation and combination program in c# 
Csharp :: how to reset disk permission 
Csharp :: c# (sharp) varibles 
Csharp :: c sharp if statements 
Csharp :: c# Detect Cycle in a Directed Graph 
Csharp :: range to 01 
Csharp :: c# string .contains against empty string returns 
Csharp :: viewsheet location revit api 
Csharp :: Wait some seconds without blocking UI execution 
Csharp :: two question marks c# 
Csharp :: how to make your player movr the way you are rotated in unity 
Csharp :: c# unhandled exception in thread” 
Csharp :: how to change samesite=lax to samesite=none in asp.net 
Csharp :: wpf c# add style to object 
Csharp :: c# linq aggregate string builder 
Csharp :: c# convert string to datetime any format 
Csharp :: lexicographically sorted 
Csharp :: visual studio auto generate and setters 
Csharp :: Program to find GCD or HCF of two numbers c# 
Csharp :: DisplayUnitType revit 2022 
Csharp :: C# how to stop user type into combobox 
Csharp :: RemoveClaim 
Csharp :: identity-1.us-south.iam.test.cloud.ibm.com:443 
Csharp :: Conditional IQueryable Linq extension 
Csharp :: multiple lines in string c# parameterized 
Csharp :: attribute c# get method name reflection 
Csharp :: translate nicely between two vector3 
Csharp :: unity int to bool 
Csharp :: unity c# rate game 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =