Advertisement
Boomsma95

Tutorial Script: Playercontroller

Mar 26th, 2013
2,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.64 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     public CharacterController CharCont;
  8.     public CharacterMotor CharMotor;
  9.  
  10.     //Holder for Weapons
  11.     public Transform WalkAnimationHolder;
  12.     public Transform JumpAnimationHolder;
  13.     public Transform ADSHolder;
  14.     public Transform SwayHolder;
  15.     public Transform RecoilHolder;
  16.     public Transform RecoilHolderCamera;
  17.     public Transform PlayerCamera;
  18.  
  19.     public WalkingState walkingstate = WalkingState.Idle;
  20.  
  21.     public float VelocityMagnitude;
  22.  
  23.     public float WalkSpeed;
  24.     public float RunSpeed;
  25.  
  26.     public bool WasStanding;
  27.  
  28.     //Player Variables
  29.     public bool IsAiming;
  30.  
  31.     public WeaponInfo CurrentWeapon;
  32.     public List<WeaponInfo> WeaponList = new List<WeaponInfo>();
  33.  
  34.     public Vector3 CurrentRecoil1;
  35.     public Vector3 CurrentRecoil2;
  36.     public Vector3 CurrentRecoil3;
  37.     public Vector3 CurrentRecoil4;
  38.  
  39.     private float shoottime = 0;
  40.  
  41.     void Start()
  42.     {
  43.         CurrentWeapon = WeaponList[0];
  44.     }
  45.  
  46.     public void Update()
  47.     {
  48.         ShootController();
  49.     }
  50.    
  51.     public void FixedUpdate()
  52.     {
  53.         AnimationController();
  54.         SwayController();
  55.         SpeedController();
  56.         RecoilController();
  57.         ADSController();
  58.         VelocityMagnitude = CharCont.velocity.magnitude;
  59.     }
  60.  
  61.     public void SpeedController()
  62.     {
  63.         if ((Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) && VelocityMagnitude > 0)
  64.         {
  65.             if (Input.GetButton("Run"))
  66.             {
  67.                 walkingstate = WalkingState.Running;
  68.                 CharMotor.movement.maxForwardSpeed = RunSpeed;
  69.                 CharMotor.movement.maxSidewaysSpeed = RunSpeed;
  70.                 CharMotor.movement.maxBackwardsSpeed = RunSpeed / 2;
  71.             }
  72.             else
  73.             {
  74.                 walkingstate = WalkingState.Walking;
  75.                 CharMotor.movement.maxForwardSpeed = WalkSpeed;
  76.                 CharMotor.movement.maxSidewaysSpeed = WalkSpeed;
  77.                 CharMotor.movement.maxBackwardsSpeed = WalkSpeed / 2;
  78.             }
  79.         }
  80.         else
  81.         {
  82.             walkingstate = WalkingState.Idle;
  83.         }
  84.     }
  85.    
  86.     public void AnimationController()
  87.     {
  88.         if (WasStanding && !CharCont.isGrounded)
  89.         {
  90.             WasStanding = false;
  91.             JumpAnimationHolder.animation.Play("WeaponJump 1");
  92.         }
  93.         else if (!WasStanding && CharCont.isGrounded)
  94.         {
  95.             WasStanding = true;
  96.             JumpAnimationHolder.animation.Play("WeaponLand 1");
  97.         }
  98.         if (walkingstate == WalkingState.Running)
  99.         {
  100.             WalkAnimationHolder.animation["WeaponRun1"].speed = VelocityMagnitude / RunSpeed * 1.2f;
  101.             WalkAnimationHolder.animation.CrossFade("WeaponRun1", 0.2f);
  102.         }
  103.         else if (walkingstate == WalkingState.Walking)
  104.         {
  105.             WalkAnimationHolder.animation["WeaponRun1"].speed = VelocityMagnitude / WalkSpeed * 1.2f;
  106.             WalkAnimationHolder.animation.CrossFade("WeaponWalk1",0.2f);
  107.         }
  108.         else
  109.         {
  110.             WalkAnimationHolder.animation.CrossFade("WeaponIdle1",0.2f);
  111.         }
  112.     }
  113.    
  114.     public void SwayController()
  115.     {
  116.        
  117.     }
  118.  
  119.     public void ShootController()
  120.     {
  121.         if (Input.GetButton("Fire1") && walkingstate != WalkingState.Running)
  122.         {
  123.             if (shoottime <= Time.time)
  124.             {
  125.                 shoottime = Time.time + CurrentWeapon.firerate;
  126.                 CurrentRecoil1 += new Vector3(CurrentWeapon.RecoilRotation.x, Random.Range(-CurrentWeapon.RecoilRotation.y, CurrentWeapon.RecoilRotation.y));
  127.                 CurrentRecoil3 += new Vector3(Random.Range(-CurrentWeapon.RecoilKickback.x, CurrentWeapon.RecoilKickback.x), Random.Range(-CurrentWeapon.RecoilKickback.y, CurrentWeapon.RecoilKickback.y), CurrentWeapon.RecoilKickback.z);
  128.  
  129.                 /*RaycastHit hit;
  130.                 if (Physics.Raycast(CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.TransformDirection(Vector3.forward), out hit, 250))
  131.                 {
  132.                     hit.transform.SendMessageUpwards("GetBulletDamage", CurrentWeapon.name, SendMessageOptions.DontRequireReceiver);
  133.                 }*/
  134.                 GameObject inst_bullet = Instantiate(CurrentWeapon.Bullet, CurrentWeapon.Spawnpoint.position, CurrentWeapon.Spawnpoint.rotation) as GameObject;
  135.                 inst_bullet.rigidbody.AddRelativeForce(Vector3.forward, ForceMode.Impulse);
  136.                 Physics.IgnoreCollision(inst_bullet.collider, CharCont);
  137.                 inst_bullet.GetComponent<BulletManager>().damage = CurrentWeapon.weaponDamage;
  138.             }
  139.         }
  140.     }
  141.  
  142.     public void RecoilController()
  143.     {
  144.         CurrentRecoil1 = Vector3.Lerp(CurrentRecoil1, Vector3.zero, 0.1f);
  145.         CurrentRecoil2 = Vector3.Lerp(CurrentRecoil2, CurrentRecoil1, 0.1f);
  146.         CurrentRecoil3 = Vector3.Lerp(CurrentRecoil3, Vector3.zero, 0.1f);
  147.         CurrentRecoil4 = Vector3.Lerp(CurrentRecoil4, CurrentRecoil3, 0.1f);
  148.  
  149.         RecoilHolder.localEulerAngles = CurrentRecoil2;
  150.         RecoilHolder.localPosition = CurrentRecoil4;
  151.  
  152.         RecoilHolderCamera.localEulerAngles = CurrentRecoil2 / 1.2f;
  153.     }
  154.  
  155.     public void ADSController()
  156.     {
  157.         if (Input.GetButton("Fire2"))
  158.         {
  159.             IsAiming = true;
  160.             ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, CurrentWeapon.Scopes[CurrentWeapon.CurrentScope].adsposition, 0.25f);
  161.             PlayerCamera.camera.fieldOfView = Mathf.Lerp(PlayerCamera.camera.fieldOfView, CurrentWeapon.Scopes[CurrentWeapon.CurrentScope].fov, 0.25f);
  162.         }
  163.         else
  164.         {
  165.             IsAiming = false;
  166.             ADSHolder.localPosition = Vector3.Lerp(ADSHolder.localPosition, Vector3.zero, 0.25f);
  167.             PlayerCamera.camera.fieldOfView = Mathf.Lerp(PlayerCamera.camera.fieldOfView, 60, 0.25f);
  168.         }
  169.     }
  170. }
  171.  
  172. public enum WalkingState
  173. {
  174.     Idle,
  175.     Walking,
  176.     Running
  177. }
  178.  
  179. [System.Serializable]
  180. public class WeaponInfo
  181. {
  182.     public string name = "Weapon";
  183.     public float firerate = 0.1f;
  184.     public float bulletSpeed = 1;
  185.     public float weaponDamage = 25;
  186.  
  187.     public Transform WeaponTransform;
  188.  
  189.     public Vector3 RecoilRotation;
  190.     public Vector3 RecoilKickback;
  191.  
  192.     public Transform Spawnpoint;
  193.     public GameObject Bullet;
  194.  
  195.     public int CurrentScope;
  196.     public List<WeaponScope> Scopes = new List<WeaponScope>();
  197. }
  198.  
  199. [System.Serializable]
  200. public class WeaponScope
  201. {
  202.     public string name;
  203.     public float fov;
  204.     public Vector3 adsposition;
  205.     public Transform scopetransform;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement