Guest User

Untitled

a guest
May 5th, 2022
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CharacterController : MonoBehaviour
  6. {
  7.     private float yaw = 0.0f, pitch = 0.0f;
  8.     private Rigidbody rb;
  9.  
  10.     private bool isSprinting = false;
  11.  
  12.     float walkSpeed = 7.0f, sensitivity = 2.0f;
  13.  
  14.     void Start()
  15.     {
  16.         Cursor.lockState = CursorLockMode.Locked;
  17.         rb = gameObject.GetComponent<Rigidbody>();
  18.     }
  19.  
  20.     void Update()
  21.     {
  22.         if (Input.GetKey(KeyCode.Space) && Physics.Raycast(rb.transform.position, Vector3.down, 1 + 0.001f))
  23.             rb.velocity = new Vector3(rb.velocity.x, 7.0f, rb.velocity.z);
  24.         Look();
  25.     }
  26.  
  27.     private void FixedUpdate()
  28.     {
  29.         Movement();
  30.     }
  31.  
  32.     void Look()
  33.     {
  34.         pitch -= Input.GetAxisRaw("Mouse Y") * sensitivity;
  35.         pitch = Mathf.Clamp(pitch, -90.0f, 90.0f);
  36.         yaw += Input.GetAxisRaw("Mouse X") * sensitivity;
  37.         Camera.main.transform.localRotation = Quaternion.Euler(pitch, yaw, 0);
  38.     }
  39.  
  40.     void Movement()
  41.     {
  42.         Vector2 axis = new Vector2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")) * walkSpeed;
  43.         Vector3 forward = new Vector3(-Camera.main.transform.right.z, 0.0f, Camera.main.transform.right.x);
  44.         Vector3 wishDirection = (forward * axis.x + Camera.main.transform.right * axis.y + Vector3.up * rb.velocity.y);
  45.         rb.velocity = wishDirection;
  46.  
  47.         if (Input.GetKey(KeyCode.LeftShift))
  48.         {
  49.             isSprinting = true;
  50.         }
  51.         else
  52.         {
  53.             isSprinting = false;
  54.         }
  55.  
  56.         if (isSprinting == true)
  57.         {
  58.             walkSpeed = 10.0f;
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment