Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour {
- // Input Keys
- public KeyCode Fire_1;
- public KeyCode Fire_2;
- public KeyCode Reload;
- public KeyCode Forward;
- public KeyCode Backward;
- public KeyCode Left;
- public KeyCode Right;
- public KeyCode Run;
- public KeyCode Jump;
- public KeyCode Crouch;
- // Params
- public float MoveSpeed;
- public float RunMultiplicator;
- public float RunEnduCost;
- public float CrouchMultiplicator;
- public float JumpPower;
- public float JumpEnduCost;
- public float TurnSpeed;
- public float TurnLerpSpeed;
- public float Drag;
- public float AirDrag;
- public AudioClip[] FootstepSound;
- public AudioClip JumpSound;
- public AudioClip LandSound;
- public float StepSpeed;
- //Components
- public Rigidbody RB;
- public Transform HeadTransform; // Head
- public float BodyYaw;
- public float HeadPitch;
- public cursor _cursor;
- public AudioSource AS;
- public weapon Primary_Weapon;
- public weapon Secondary_Weapon;
- public CapsuleCollider HitBox;
- public endurance Endu;
- private Vector3 Force;
- private bool isLanded;
- private float stepDelay;
- void Update () {
- Movement();
- WeaponControl();
- SoundControl();
- }
- void FixedUpdate() {
- Rotation();
- }
- void WeaponControl() {
- if( _cursor.CursorActive ) {
- return;
- }
- if( Primary_Weapon ) {
- if( Input.GetKey( Fire_1 ) ) {
- Primary_Weapon.FireWeapon();
- }
- if( Input.GetKeyDown( Reload ) ) {
- Primary_Weapon.Reloading();
- }
- }
- if( Secondary_Weapon ) {
- if( Input.GetKey( Fire_2 ) ) {
- Secondary_Weapon.FireWeapon();
- }
- }
- }
- void Movement() {
- if( !isGrounded() ) {
- RB.velocity = new Vector3( RB.velocity.x * AirDrag, RB.velocity.y, RB.velocity.z * AirDrag );
- return;
- }
- Vector3 MoveDir = Vector3.zero;
- if( Input.GetKey( Forward ) ) {
- MoveDir.z = 1;
- }
- if( Input.GetKey( Backward ) ) {
- MoveDir.z = -1;
- }
- if( Input.GetKey( Left ) ) {
- MoveDir.x = -1;
- }
- if( Input.GetKey( Right ) ) {
- MoveDir.x = 1;
- }
- MoveDir = MoveDir.normalized * MoveSpeed;
- if( Input.GetKey( Run ) ) {
- if( Endu.Endurance( RunEnduCost ) ) {
- MoveDir *= RunMultiplicator;
- }
- }
- if( Input.GetKey( Crouch ) ) {
- MoveDir *= CrouchMultiplicator;
- }
- if( Input.GetKeyDown( Crouch ) ) {
- HitBox.height = 1f;
- HitBox.transform.localPosition = new Vector3( 0f, 1.2f, 0f );
- }
- if( Input.GetKeyUp( Crouch ) ) {
- HitBox.height = 2f;
- HitBox.transform.localPosition = new Vector3( 0f, 0.9f, 0f );
- }
- if( Input.GetKeyDown( Jump ) ) {
- if( Endu.Endurance( JumpEnduCost ) ) {
- MoveDir.y = JumpPower;
- PlaySound( JumpSound );
- }
- }
- Force = Vector3.zero;
- if( !_cursor.CursorActive ) {
- Force += transform.forward * MoveDir.z;
- Force += transform.right * MoveDir.x;
- Force += Vector3.up * ( RB.velocity.y + MoveDir.y );
- }
- if( Force == Vector3.zero ) {
- RB.velocity = new Vector3( RB.velocity.x * Drag, RB.velocity.y - 0.1f, RB.velocity.z * Drag );
- } else {
- RB.velocity = new Vector3( Force.x, Force.y, Force.z );
- }
- }
- void Rotation() {
- if( _cursor.CursorActive ) {
- return;
- }
- BodyYaw += Input.GetAxis( "Mouse X" ) * Time.fixedDeltaTime * TurnSpeed;
- if( BodyYaw < 0f ) {
- BodyYaw = 360f + BodyYaw;
- }
- if( BodyYaw > 360f ) {
- BodyYaw = 0f + ( BodyYaw - 360f);
- }
- HeadPitch -= Input.GetAxis( "Mouse Y" ) * Time.fixedDeltaTime * TurnSpeed;
- HeadPitch = Mathf.Clamp( HeadPitch, -89f, 89f );
- transform.rotation = Quaternion.Lerp( transform.rotation, Quaternion.Euler( 0, BodyYaw, 0 ), TurnLerpSpeed * Time.fixedDeltaTime );
- HeadTransform.localRotation = Quaternion.Lerp( transform.rotation, Quaternion.Euler( HeadPitch, 0, 0 ), TurnLerpSpeed * Time.fixedDeltaTime );
- }
- bool isGrounded() {
- RaycastHit GroundHit;
- Ray GroundRay = new Ray( transform.position + transform.up, -Vector3.up );
- if( Physics.SphereCast( GroundRay ,0.2f, out GroundHit, 1.1f ) ) {
- if( GroundHit.collider ) {
- return true;
- }
- }
- return false;
- }
- void SoundControl() {
- if( isGrounded() ) {
- //if( Input.GetKeyDown( Jump ) ) {
- // PlaySound( JumpSound );
- //}
- if( !isLanded ) {
- isLanded = true;
- PlaySound( LandSound );
- }
- stepDelay -= Time.deltaTime * RB.velocity.magnitude * StepSpeed;
- if( stepDelay <= 0f ) {
- stepDelay = 1f;
- PlaySound( FootstepSound[Random.Range(0,FootstepSound.Length)] );
- }
- } else {
- isLanded = false;
- }
- }
- void PlaySound( AudioClip clip ) {
- if( !AS ) {
- return;
- }
- if( !clip ) {
- return;
- }
- AS.PlayOneShot( clip );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement