Advertisement
VortX7

Character Controller script for Unity 3D Game

Sep 21st, 2023
1,639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | Gaming | 0 0
  1. //Start of Movement script
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class Movement : MonoBehaviour
  7. {
  8.     [SerializeField] bool grounded = false;
  9.     [SerializeField] CharacterController controller;
  10.     [SerializeField] float speed = 10f;//ground speed
  11.     [SerializeField] float jHeight = 1.5f;//jump height
  12.     [SerializeField] Vector3 crouch = new Vector3(1f, 0.5f, 1f);
  13.     [SerializeField] float crspd = 4.5f;
  14.     [SerializeField] float sia = 5.7325f;//speed in air
  15.     float ground_dist;
  16.     float x, y, z;
  17.     Vector3 velocity;
  18.     [SerializeField] float gravity = -9.81f;
  19.     // Update is called once per frame
  20.     void Update()
  21.     {
  22.         if(grounded && velocity.y<0)
  23.         {
  24.             velocity.y = 0;
  25.         }
  26.         x = Input.GetAxis("Horizontal");
  27.         y = Input.GetAxis("Jump");
  28.         z = Input.GetAxis("Vertical");
  29.         ground_dist = 1f + 0.08f;
  30.         if (Physics.Raycast(transform.position, -transform.up, out _, ground_dist))
  31.         {
  32.             grounded = true;
  33.         }
  34.         else
  35.         {
  36.             grounded = false;
  37.         }
  38.         Vector3 move = transform.right * x + transform.forward * z;
  39.         if(Input.GetKey(KeyCode.LeftControl) && z==1f && grounded)//Sprint
  40.         {
  41.             controller.Move(speed * 1.5f * Time.deltaTime * move);
  42.         }
  43.         if(grounded)
  44.         {
  45.             controller.Move(speed * Time.deltaTime * move);
  46.         }
  47.         if(!grounded)
  48.         {
  49.             controller.Move(sia * Time.deltaTime * move);
  50.         }
  51.         if (y==1f && grounded)
  52.         {
  53.             velocity.y = Mathf.Sqrt(jHeight * -2f * gravity);
  54.         }
  55.         velocity.y += gravity * Time.deltaTime;
  56.         controller.Move(velocity * Time.deltaTime);
  57.     }
  58. }
  59. //End of movement script
  60.  
  61. //Start of vision script
  62. using System.Collections;
  63. using System.Collections.Generic;
  64. using UnityEngine;
  65.  
  66. public class Vision : MonoBehaviour
  67. {
  68.     [SerializeField] private float sens = 100f;
  69.     [SerializeField] Transform player;
  70.     float xrotation = 0f;
  71.     // Start is called before the first frame update
  72.     void Start()
  73.     {
  74.         Cursor.lockState = CursorLockMode.Locked;  
  75.     }
  76.  
  77.     // Update is called once per frame
  78.     void Update()
  79.     {
  80.         float x = Input.GetAxis("Mouse X") * sens * Time.deltaTime;
  81.         float y = Input.GetAxis("Mouse Y") * sens * Time.deltaTime;
  82.         xrotation -= y;
  83.         xrotation = Mathf.Clamp(xrotation, -90f, 90f);
  84.         transform.localRotation = Quaternion.Euler(xrotation, 0f, 0f);
  85.         player.Rotate(Vector3.up * x);
  86.     }
  87. }
  88. //End of vision code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement