Bukyja

First Person Controller

Oct 4th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using AC;
  5.  
  6. public class ControllerPlayer : MonoBehaviour
  7. {
  8.     Rigidbody rb;
  9.     private float speed = 40f;
  10.     private float rotationSpeed = 50f;
  11.     public bool IsCrouching;
  12.     public bool IsPressedS;
  13.     private Player acPlayer;
  14.     private Char character;
  15.     Animation camAnim;
  16.     Animator animator;
  17.     CapsuleCollider col;
  18.     Camera playerCam;
  19.  
  20.     private float speedRotation = 1f;
  21.    
  22.  
  23.     public void Start ()
  24.     {
  25.  
  26.         rb = GetComponent<Rigidbody>();
  27.         animator = GetComponent<Animator>();
  28.         //controller = GetComponent<CharacterController>();
  29.         col = GetComponent<CapsuleCollider>();
  30.         playerCam = GetComponentInChildren<Camera>();
  31.         camAnim = GetComponentInChildren<Animation>();
  32.         acPlayer = GetComponent<AC.Player>();
  33.         character = GetComponent<AC.Char>();
  34.         IsCrouching = false;
  35.         IsPressedS = false;
  36.        
  37.        
  38.         //StartCoroutine(PlayerAutomatic());
  39.      
  40.  
  41.  
  42.     }
  43.  
  44.     /*IEnumerator PlayerAutomatic()
  45.     {
  46.         AC.KickStarter.player.motionControl = AC.MotionControl.Automatic;
  47.         yield return new WaitForSeconds(0.1f);
  48.         AC.KickStarter.player.motionControl = AC.MotionControl.Manual;
  49.     }
  50.  
  51.     */
  52.  
  53.  
  54.     public void LateUpdate()
  55.    
  56.  
  57.        
  58.         #region Movimento fuori da AC - sennΓ² utilizza AC solo per vertical e horizontal
  59.     {
  60.  
  61.         float translation = Input.GetAxis("Vertical") * speed;
  62.         float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
  63.  
  64.  
  65.         translation *= speed * Time.deltaTime;
  66.         rotation *= rotationSpeed * Time.deltaTime;
  67.         Quaternion turn = Quaternion.Euler(0f, rotation, 0f);
  68.         rb.MoveRotation(rb.rotation * turn);
  69.         animator.SetFloat("vertical", translation, 0.1f, Time.deltaTime * 10);
  70.         animator.SetFloat("horizontal", rotation, 0.1f, Time.deltaTime * 10);
  71.         #endregion
  72.  
  73.         #region attiva crouch + modifica dimensione collider + modifica posizione camera + modifica velocitΓ  corsa
  74.  
  75.  
  76.         if (IsCrouching == false && Input.GetKeyDown(KeyCode.C))
  77.  
  78.         {
  79.              animator.SetBool("crouch", true);
  80.              IsCrouching = true;
  81.              col.height = 1.1f;
  82.              col.center = new Vector3(0f, 0.5f, 0f);
  83.              camAnim.Play("CamAnimDown");
  84.              character.runSpeedScale = 2f;
  85.              character.walkSpeedScale = 1f;
  86.          
  87.  
  88.         }
  89.  
  90.          else if (IsCrouching == true && Input.GetKeyDown(KeyCode.C))
  91.  
  92.         {
  93.  
  94.              animator.SetBool("crouch", false);
  95.              IsCrouching = false;
  96.              col.height = 1.58f;
  97.              col.center = new Vector3(0f, 0.77f, 0f);
  98.              camAnim.Play("CamAnimUp");
  99.              character.runSpeedScale = 4f;
  100.              character.walkSpeedScale = 2f;
  101.  
  102.         }
  103.  
  104.  
  105.        
  106.  
  107.         if (IsCrouching == false && (IsPressedS == false) && Input.GetKey(KeyCode.S))
  108.  
  109.         {
  110.             IsPressedS = true;
  111.             character.runSpeedScale = 2f;
  112.             character.walkSpeedScale = 1f;
  113.  
  114.         }
  115.  
  116.         else if (IsPressedS == true && Input.GetKeyUp(KeyCode.S))
  117.  
  118.         {
  119.             IsPressedS = false;
  120.             character.runSpeedScale = 4f;
  121.             character.walkSpeedScale = 2f;
  122.         }
  123.  
  124.  
  125.         if (IsCrouching == false && (IsPressedS == true) && Input.GetKeyDown(KeyCode.LeftAlt))
  126.  
  127.         {
  128.  
  129.             this.transform.Rotate(0, 180 * Time.deltaTime, 0, Space.Self);
  130.            
  131.         }
  132.  
  133.         #endregion
  134.  
  135.  
  136.  
  137.     }
  138.  
  139.  
  140.  
  141. }
  142.  
  143.  
  144.  
  145.  
  146. /*
  147. this.transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0, 0, 0), speedRotation * Time.deltaTime);
  148. */
Advertisement
Add Comment
Please, Sign In to add comment