Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class PlayerController : MonoBehaviour
- {
- public CharacterController CharCont;
- public CharacterMotor CharMotor;
- //Holder for Weapons
- public Transform WalkAnimationHolder;
- public Transform JumpAnimationHolder;
- public Transform ADSHolder;
- public Transform SwayHolder;
- public Transform RecoilHolder;
- public Transform RecoilHolderCamera;
- public Transform PlayerCamera;
- public WalkingState walkingstate = WalkingState.Idle;
- public float VelocityMagnitude;
- public float WalkSpeed;
- public float RunSpeed;
- public bool WasStanding;
- //Player Variables
- public bool IsAiming;
- public WeaponInfo CurrentWeapon;
- public List<WeaponInfo> WeaponList = new List<WeaponInfo>();
- public Vector3 CurrentRecoil1;
- public Vector3 CurrentRecoil2;
- public Vector3 CurrentRecoil3;
- public Vector3 CurrentRecoil4;
- private float shoottime = 0;
- void Start()
- {
- CurrentWeapon = WeaponList[0];
- }
- public void Update()
- {
- ShootController();
- }
- public void FixedUpdate()
- {
- AnimationController();
- SwayController();
- SpeedController();
- RecoilController();
- ADSController();
- VelocityMagnitude = CharCont.velocity.magnitude;
- }
- public void SpeedController()
- {
- if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && VelocityMagnitude > 0)
- {
- if (Input.GetButton("Run"))
- {
- walkingstate = WalkingState.Running;
- CharMotor.movement.maxForwardSpeed = RunSpeed;
- CharMotor.movement.maxSidewaysSpeed = RunSpeed;
- CharMotor.movement.maxBackwardsSpeed = RunSpeed / 2;
- }
- else
- {
- walkingstate = WalkingState.Walking;
- CharMotor.movement.maxForwardSpeed = WalkSpeed;
- CharMotor.movement.maxSidewaysSpeed = WalkSpeed;
- CharMotor.movement.maxBackwardsSpeed = WalkSpeed / 2;
- }
- }
- else
- {
- walkingstate = WalkingState.Idle;
- }
- }
- public void AnimationController()
- {
- if (WasStanding && !CharCont.isGrounded)
- {
- WasStanding = false;
- JumpAnimationHolder.animation.Play("WeaponJump 1");
- }
- else if (!WasStanding && CharCont.isGrounded)
- {
- WasStanding = true;
- JumpAnimationHolder.animation.Play("WeaponLand 1");
- }
- if (walkingstate == WalkingState.Running)
- {
- WalkAnimationHolder.animation["WeaponRun1"].speed = VelocityMagnitude / RunSpeed * 1.2f;
- WalkAnimationHolder.animation.CrossFade("WeaponRun1", 0.2f);
- }
- else if (walkingstate == WalkingState.Walking)
- {
- WalkAnimationHolder.animation["WeaponRun1"].speed = VelocityMagnitude / WalkSpeed * 1.2f;
- WalkAnimationHolder.animation.CrossFade("WeaponWalk1",0.2f);
- }
- else
- {
- WalkAnimationHolder.animation.CrossFade("WeaponIdle1",0.2f);
- }
- }
- public void SwayController()
- {
- }
- public void ShootController()
- {
- if (Input.GetButton("Fire1") && walkingstate != WalkingState.Running)
- {
- if (shoottime <= Time.time)
- {
- shoottime = Time.time + CurrentWeapon.firerate;
- CurrentRecoil1 += new Vector3(CurrentWeapon.RecoilRotation.x, Random.Range(-CurrentWeapon.RecoilRotation.y, CurrentWeapon.RecoilRotation.y));
- CurrentRecoil3 += new Vector3(Random.Range(-CurrentWeapon.RecoilKickback.x, CurrentWeapon.RecoilKickback.x), Random.Range(-CurrentWeapon.RecoilKickback.y, CurrentWeapon.RecoilKickback.y), CurrentWeapon.RecoilKickback.z);
- /*RaycastHit hit;
- if (Physics.Raycast(CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.TransformDirection(Vector3.forward), out hit, 250))
- {
- hit.transform.SendMessageUpwards("GetBulletDamage", CurrentWeapon.name, SendMessageOptions.DontRequireReceiver);
- }*/
- GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
- inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
- Physics.IgnoreCollision(inst_bullet.collider, CharCont);
- inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
- }
- }
- }
- public void RecoilController()
- {
- CurrentRecoil1 = Vector3.Lerp(CurrentRecoil1, Vector3.zero, 0.1f);
- CurrentRecoil2 = Vector3.Lerp(CurrentRecoil2, CurrentRecoil1, 0.1f);
- CurrentRecoil3 = Vector3.Lerp(CurrentRecoil3, Vector3.zero, 0.1f);
- CurrentRecoil4 = Vector3.Lerp(CurrentRecoil4, CurrentRecoil3, 0.1f);
- RecoilHolder.localEulerAngles = CurrentRecoil2;
- RecoilHolder.localPosition = CurrentRecoil4;
- RecoilHolderCamera.localEulerAngles = CurrentRecoil2 / 1.2f;
- }
- public void ADSController()
- {
- if (Input.GetButton("Fire2"))
- {
- IsAiming = true;
- ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, CurrentWeapon.Scopes[CurrentWeapon.CurrentScope].adsposition, 0.25f);
- PlayerCamera.camera.fieldOfView = Mathf.Lerp(PlayerCamera.camera.fieldOfView, CurrentWeapon.Scopes[CurrentWeapon.CurrentScope].fov, 0.25f);
- }
- else
- {
- IsAiming = false;
- ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, Vector3.zero, 0.25f);
- PlayerCamera.camera.fieldOfView = Mathf.Lerp(PlayerCamera.camera.fieldOfView, 60, 0.25f);
- }
- }
- }
- public enum WalkingState
- {
- Idle,
- Walking,
- Running
- }
- [System.Serializable]
- public class WeaponInfo
- {
- public string name = "Weapon";
- public float firerate = 0.1f;
- public float bulletSpeed = 1;
- public float weaponDamage = 25;
- public Transform WeaponTransform;
- public Vector3 RecoilRotation;
- public Vector3 RecoilKickback;
- public Transform Spawnpoint;
- public GameObject Bullet;
- public int CurrentScope;
- public List<WeaponScope> Scopes = new List<WeaponScope>();
- }
- [System.Serializable]
- public class WeaponScope
- {
- public string name;
- public float fov;
- public Vector3 adsposition;
- public Transform scopetransform;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement