Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour {
  6.  
  7.     // Input Keys
  8.     public KeyCode Fire_1;
  9.     public KeyCode Fire_2;
  10.     public KeyCode Reload;
  11.     public KeyCode Forward;
  12.     public KeyCode Backward;
  13.     public KeyCode Left;
  14.     public KeyCode Right;
  15.     public KeyCode Run;
  16.     public KeyCode Jump;
  17.     public KeyCode Crouch;
  18.  
  19.     // Params
  20.     public float MoveSpeed;
  21.     public float RunMultiplicator;
  22.     public float RunEnduCost;
  23.     public float CrouchMultiplicator;
  24.     public float JumpPower;
  25.     public float JumpEnduCost;
  26.     public float TurnSpeed;
  27.     public float TurnLerpSpeed;
  28.     public float Drag;
  29.     public float AirDrag;
  30.     public AudioClip[] FootstepSound;
  31.     public AudioClip JumpSound;
  32.     public AudioClip LandSound;
  33.     public float StepSpeed;
  34.  
  35.     //Components
  36.     public Rigidbody RB;
  37.     public Transform HeadTransform; // Head
  38.     public float BodyYaw;
  39.     public float HeadPitch;
  40.     public cursor _cursor;
  41.     public AudioSource AS;
  42.     public weapon Primary_Weapon;
  43.     public weapon Secondary_Weapon;
  44.     public CapsuleCollider HitBox;
  45.     public endurance Endu;
  46.  
  47.     private Vector3 Force;
  48.     private bool isLanded;
  49.     private float stepDelay;
  50.    
  51.  
  52.     void Update () {
  53.         Movement();
  54.         WeaponControl();
  55.         SoundControl();
  56.     }
  57.  
  58.     void FixedUpdate() {
  59.         Rotation();
  60.     }
  61.  
  62.     void WeaponControl() {
  63.         if( _cursor.CursorActive ) {
  64.             return;
  65.         }
  66.         if( Primary_Weapon ) {
  67.             if( Input.GetKey( Fire_1 ) ) {
  68.                 Primary_Weapon.FireWeapon();
  69.             }
  70.             if( Input.GetKeyDown( Reload ) ) {
  71.                 Primary_Weapon.Reloading();
  72.             }
  73.         }
  74.         if( Secondary_Weapon ) {
  75.             if( Input.GetKey( Fire_2 ) ) {
  76.                 Secondary_Weapon.FireWeapon();
  77.             }
  78.         }
  79.        
  80.     }
  81.  
  82.     void Movement() {
  83.         if( !isGrounded() ) {
  84.             RB.velocity = new Vector3( RB.velocity.x * AirDrag, RB.velocity.y, RB.velocity.z * AirDrag );
  85.             return;
  86.         }
  87.         Vector3 MoveDir = Vector3.zero;
  88.  
  89.         if( Input.GetKey( Forward ) ) {
  90.             MoveDir.z = 1;
  91.         }
  92.         if( Input.GetKey( Backward ) ) {
  93.             MoveDir.z = -1;
  94.         }
  95.         if( Input.GetKey( Left ) ) {
  96.             MoveDir.x = -1;
  97.         }
  98.         if( Input.GetKey( Right ) ) {
  99.             MoveDir.x = 1;
  100.         }
  101.  
  102.         MoveDir = MoveDir.normalized * MoveSpeed;
  103.  
  104.         if( Input.GetKey( Run ) ) {
  105.             if( Endu.Endurance( RunEnduCost ) ) {
  106.                 MoveDir *= RunMultiplicator;
  107.             }          
  108.         }
  109.         if( Input.GetKey( Crouch ) ) {
  110.             MoveDir *= CrouchMultiplicator;
  111.         }
  112.         if( Input.GetKeyDown( Crouch ) ) {
  113.             HitBox.height = 1f;
  114.             HitBox.transform.localPosition = new Vector3( 0f, 1.2f, 0f );
  115.         }
  116.         if( Input.GetKeyUp( Crouch ) ) {
  117.             HitBox.height = 2f;
  118.             HitBox.transform.localPosition = new Vector3( 0f, 0.9f, 0f );
  119.         }
  120.  
  121.         if( Input.GetKeyDown( Jump ) ) {
  122.             if( Endu.Endurance( JumpEnduCost ) ) {
  123.                 MoveDir.y = JumpPower;
  124.                 PlaySound( JumpSound );
  125.             }
  126.         }
  127.         Force = Vector3.zero;
  128.  
  129.         if( !_cursor.CursorActive ) {
  130.             Force += transform.forward * MoveDir.z;
  131.             Force += transform.right * MoveDir.x;
  132.             Force += Vector3.up * ( RB.velocity.y + MoveDir.y );
  133.         }
  134.  
  135.         if( Force == Vector3.zero ) {
  136.             RB.velocity = new Vector3( RB.velocity.x * Drag, RB.velocity.y - 0.1f, RB.velocity.z * Drag );
  137.         } else {
  138.             RB.velocity = new Vector3( Force.x, Force.y, Force.z );
  139.         }
  140.        
  141.     }
  142.  
  143.     void Rotation() {
  144.         if( _cursor.CursorActive ) {
  145.             return;
  146.         }
  147.         BodyYaw += Input.GetAxis( "Mouse X" ) * Time.fixedDeltaTime * TurnSpeed;
  148.         if( BodyYaw < 0f ) {
  149.             BodyYaw = 360f + BodyYaw;
  150.         }
  151.         if( BodyYaw > 360f ) {
  152.             BodyYaw = 0f + ( BodyYaw - 360f);
  153.         }
  154.         HeadPitch -= Input.GetAxis( "Mouse Y" ) * Time.fixedDeltaTime * TurnSpeed;
  155.         HeadPitch = Mathf.Clamp( HeadPitch, -89f, 89f );
  156.  
  157.         transform.rotation = Quaternion.Lerp( transform.rotation, Quaternion.Euler( 0, BodyYaw, 0 ), TurnLerpSpeed * Time.fixedDeltaTime );
  158.         HeadTransform.localRotation = Quaternion.Lerp( transform.rotation, Quaternion.Euler( HeadPitch, 0, 0 ), TurnLerpSpeed * Time.fixedDeltaTime );
  159.     }
  160.  
  161.     bool isGrounded() {
  162.         RaycastHit GroundHit;
  163.         Ray GroundRay = new Ray( transform.position + transform.up, -Vector3.up );
  164.  
  165.         if( Physics.SphereCast( GroundRay ,0.2f, out GroundHit, 1.1f ) ) {
  166.             if( GroundHit.collider ) {
  167.                 return true;
  168.             }
  169.            
  170.         }
  171.         return false;
  172.     }
  173.  
  174.     void SoundControl() {
  175.         if( isGrounded() ) {
  176.             //if( Input.GetKeyDown( Jump ) ) {
  177.             //    PlaySound( JumpSound );
  178.             //}
  179.             if( !isLanded ) {
  180.                 isLanded = true;
  181.                 PlaySound( LandSound );
  182.             }
  183.  
  184.             stepDelay -= Time.deltaTime * RB.velocity.magnitude * StepSpeed;
  185.  
  186.             if( stepDelay <= 0f ) {
  187.                 stepDelay = 1f;
  188.                 PlaySound( FootstepSound[Random.Range(0,FootstepSound.Length)] );
  189.             }
  190.         } else {
  191.             isLanded = false;
  192.         }
  193.     }
  194.  
  195.     void PlaySound( AudioClip clip ) {
  196.         if( !AS ) {
  197.             return;
  198.         }
  199.         if( !clip ) {
  200.             return;
  201.         }
  202.         AS.PlayOneShot( clip );
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement