Advertisement
Diamond32_Tutoriales

PlayerController

Jun 20th, 2021 (edited)
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ControlFreak2;
  5.  
  6. public class PlayerController : MonoBehaviour
  7. {
  8.     [Header("Player Options")]
  9.     [Range(0, 100)] public float WalkSpeed;
  10.     CharacterController cc;
  11.  
  12.  
  13.     [Header("Rotate Camera Options")]
  14.     public Camera cam;
  15.     [Range(0, 200)] public float CameraSensibilidad;
  16.     float vMouse;
  17.     float hMouse;
  18.  
  19.     [Header("Fall Options")]
  20.     [Range(0, 20)] public float SpeedControllerInFall;
  21.     [Range(0, 20)] public float SmoothFallGravity;
  22.  
  23.     [Header("Check Ground Options")]
  24.     public bool isGround;
  25.     public Transform CheckFloor;
  26.     [Range(0, 20)] public float CheckRadius;
  27.     [Range(0, 500)] public float gravedad;
  28.     public LayerMask mask;
  29.  
  30.     [Header("Extras Options")]
  31.     public float SmoothTimeRotateLuz;
  32.     public Transform Luz;
  33.  
  34.     void Start()
  35.     {
  36.         cc = GetComponent<CharacterController>();
  37.  
  38.         Application.targetFrameRate = 60;
  39.     }
  40.  
  41.     void Update()
  42.     {
  43.         isGround = Physics.CheckSphere(CheckFloor.position, CheckRadius, mask);
  44.  
  45.         var h = CF2Input.GetAxis("Horizontal");
  46.         var v = CF2Input.GetAxis("Vertical");
  47.         Vector3 move = new Vector3(h, 0f, v);
  48.         vMouse -= Time.deltaTime * CF2Input.GetAxis("Mouse Y") * 1 * CameraSensibilidad;
  49.         hMouse += Time.deltaTime * CF2Input.GetAxis("Mouse X") * 1 * CameraSensibilidad;
  50.  
  51.         if (isGround)
  52.         {
  53.             this.transform.rotation = Quaternion.Euler(0f, hMouse, 0f);
  54.             cam.transform.localEulerAngles = new Vector3(vMouse, 0f, 0f);
  55.             move.y -= gravedad * Time.deltaTime * 1f;
  56.  
  57.             move = this.transform.TransformDirection(move);
  58.             cc.Move(move * WalkSpeed * Time.deltaTime * 1);
  59.         }
  60.         else
  61.         {
  62.             this.transform.rotation = Quaternion.Euler(0f, hMouse, 0f);
  63.             cam.transform.localEulerAngles = new Vector3(vMouse, 0f, 0f);
  64.             move.y -= gravedad * Time.deltaTime * 1f / SmoothFallGravity;
  65.  
  66.  
  67.             move = this.transform.TransformDirection(move / SpeedControllerInFall);
  68.             cc.Move(move * WalkSpeed * Time.deltaTime * 1f);
  69.         }
  70.     }
  71.  
  72.     void OnDrawGizmos()
  73.     {
  74.         Gizmos.color = Color.yellow;
  75.         Gizmos.DrawSphere(CheckFloor.position, CheckRadius);
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement