Advertisement
Bukyja

First Person Controller

Oct 8th, 2019
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using AC;
  5.  
  6.  
  7. public class ControllerPlayer : MonoBehaviour
  8. {
  9.     Rigidbody rb;
  10.     public bool IsCrouching;
  11.     public bool IsPressedS;
  12.     private Player acPlayer;
  13.     private Char character;
  14.     Animation camAnim;
  15.     Animation acPlayerAnim;
  16.     Animator animator;
  17.     CapsuleCollider col;
  18.     Camera playerCam;
  19.    
  20.  
  21.     Quaternion playerAngle_180 = Quaternion.Euler (0, 180, 0);
  22.     Quaternion playerAngle_0 = Quaternion.Euler(0, 0, 0);
  23.     Quaternion currentAngle;
  24.    
  25.  
  26.     private void Start()
  27.     {
  28.  
  29.         rb = GetComponent<Rigidbody>();
  30.         animator = GetComponent<Animator>();
  31.         col = GetComponent<CapsuleCollider>();
  32.         camAnim = GetComponentInChildren<Animation>();
  33.         acPlayer = GetComponent<AC.Player>();
  34.         character = GetComponent<AC.Char>();
  35.  
  36.         IsCrouching = false;
  37.         IsPressedS = false;
  38.  
  39.         currentAngle = playerAngle_0;
  40.  
  41.     }
  42.  
  43.     #region abilita lo script AC motion control solo per posizionare il player in direzione della freccia, dopodichè lo disabilita per sfruttare il custom controller
  44.  
  45.     /*  
  46.    
  47.  
  48.       StartCoroutine(PlayerAutomatic());
  49.  
  50. {
  51.  
  52.       IEnumerator PlayerAutomatic()
  53.   {
  54.       acPlayer.motionControl = AC.MotionControl.Automatic;
  55.       yield return new WaitForSeconds(0.1f);
  56.       acPlayer.motionControl = AC.MotionControl.Manual;
  57.  
  58.   }
  59.  
  60. }
  61.   */
  62.  
  63.     #endregion
  64.  
  65.     private void LateUpdate()
  66.  
  67.  
  68.  
  69.     {
  70.  
  71.         #region attiva crouch + modifica dimensione collider + modifica posizione camera + modifica velocità corsa
  72.  
  73.  
  74.         if (IsCrouching == false && Input.GetKeyDown(KeyCode.C))
  75.  
  76.         {
  77.             animator.SetBool("crouch", true);
  78.             IsCrouching = true;
  79.             col.height = 1.1f;
  80.             col.center = new Vector3(0f, 0.5f, 0f);
  81.             camAnim.Play("CamAnimDown");
  82.             character.runSpeedScale = 2f;
  83.             character.walkSpeedScale = 1f;
  84.  
  85.  
  86.         }
  87.  
  88.         else if (IsCrouching == true && Input.GetKeyDown(KeyCode.C))
  89.  
  90.         {
  91.  
  92.             animator.SetBool("crouch", false);
  93.             IsCrouching = false;
  94.             col.height = 1.58f;
  95.             col.center = new Vector3(0f, 0.77f, 0f);
  96.             camAnim.Play("CamAnimUp");
  97.             character.runSpeedScale = 4f;
  98.             character.walkSpeedScale = 2f;
  99.  
  100.         }
  101.  
  102.         #endregion
  103.  
  104.         #region controlla se il il pulsante S è premuto, se si, in combinazione con LeftAlt ruota il player 180 gradi, se invece S viene rilasciato non fa niente.
  105.  
  106.         if (IsCrouching == false && (IsPressedS == false) && Input.GetKey(KeyCode.S))
  107.  
  108.         {
  109.             IsPressedS = true;
  110.             character.runSpeedScale = 2f;
  111.         }
  112.  
  113.         else if (IsPressedS == true && Input.GetKeyUp(KeyCode.S))
  114.  
  115.         {
  116.             IsPressedS = false;
  117.             character.runSpeedScale = 4f;
  118.         }
  119.  
  120.  
  121.  
  122.         if (IsCrouching == false && (IsPressedS == true) && Input.GetKeyDown(KeyCode.LeftAlt))
  123.  
  124.         {
  125.             {
  126.                 changeCurrentAngle();
  127.             }
  128.  
  129.             acPlayer.transform.localRotation = Quaternion.Lerp(playerAngle_0, playerAngle_180, 0.1f);
  130.  
  131.         }
  132.  
  133.         void changeCurrentAngle ()
  134.         {
  135.             if (currentAngle.eulerAngles.y == playerAngle_0.eulerAngles.y)
  136.                             acPlayer.SetRotation (playerAngle_180);
  137.  
  138.  
  139.         }
  140.  
  141.         #endregion
  142.  
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement