Advertisement
Selzier

FirstPersonController

Feb 19th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.16 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4. using UnityStandardAssets.CrossPlatformInput;
  5. using UnityStandardAssets.Utility;
  6. using Random = UnityEngine.Random;
  7.  
  8. //namespace UnityStandardAssets.Characters.FirstPerson
  9. //{
  10.     [RequireComponent(typeof (CharacterController))]
  11.     [RequireComponent(typeof (AudioSource))]
  12.     public class FirstPersonController : MonoBehaviour
  13.     {
  14.         [SerializeField] private bool m_IsWalking;
  15.         [SerializeField] private float m_WalkSpeed;
  16.         [SerializeField] private float m_RunSpeed;
  17.         [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
  18.         [SerializeField] private float m_JumpSpeed;
  19.         [SerializeField] private float m_StickToGroundForce;
  20.         [SerializeField] private float m_GravityMultiplier;
  21.         //[SerializeField] private MouseLook m_MouseLook;
  22.         [SerializeField] private bool m_UseFovKick;
  23.         [SerializeField] private FOVKick m_FovKick = new FOVKick();
  24.         [SerializeField] private bool m_UseHeadBob;
  25.         [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
  26.         [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
  27.         [SerializeField] private float m_StepInterval;
  28.         [SerializeField] private AudioClip[] m_FootstepSounds;    // an array of footstep sounds that will be randomly selected from.
  29.         [SerializeField] private AudioClip m_JumpSound;           // the sound played when character leaves the ground.
  30.         [SerializeField] private AudioClip m_LandSound;           // the sound played when character touches back on ground.
  31.  
  32.         private Camera m_Camera;
  33.         private bool m_Jump;
  34.         private float m_YRotation;
  35.         private Vector2 m_Input;
  36.         private Vector3 m_MoveDir = Vector3.zero;
  37.         private CharacterController m_CharacterController;
  38.         private CollisionFlags m_CollisionFlags;
  39.         private bool m_PreviouslyGrounded;
  40.         private Vector3 m_OriginalCameraPosition;
  41.         private float m_StepCycle;
  42.         private float m_NextStep;
  43.         private bool m_Jumping;
  44.         private AudioSource m_AudioSource;
  45.  
  46.         public bool bToggleWalk;
  47.  
  48.  
  49.     void OnEnable() {
  50.         Cardboard.SDK.OnTrigger += TriggerPulled;
  51.     }
  52.     void OnDisable() {
  53.         Cardboard.SDK.OnTrigger -= TriggerPulled;
  54.     }
  55.  
  56.     private void TriggerPulled() {
  57.         if (bToggleWalk == true) {
  58.             bToggleWalk = false;
  59.         }else {
  60.             bToggleWalk = true;
  61.         }
  62.     }
  63.  
  64.     // Use this for initialization
  65.     private void Start()
  66.         {
  67.             m_CharacterController = GetComponent<CharacterController>();
  68.             m_Camera = Camera.main;
  69.             m_OriginalCameraPosition = m_Camera.transform.localPosition;
  70.             m_FovKick.Setup(m_Camera);
  71.             m_HeadBob.Setup(m_Camera, m_StepInterval);
  72.             m_StepCycle = 0f;
  73.             m_NextStep = m_StepCycle/2f;
  74.             m_Jumping = false;
  75.             m_AudioSource = GetComponent<AudioSource>();
  76.             //m_MouseLook.Init(transform , m_Camera.transform);
  77.         }
  78.  
  79.  
  80.         // Update is called once per frame
  81.         private void Update()
  82.         {
  83.  
  84.            
  85.             RotateView();
  86.             // the jump state needs to read here to make sure it is not missed
  87.             if (!m_Jump)
  88.             {
  89.                 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
  90.             }
  91.  
  92.             if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
  93.             {
  94.                 StartCoroutine(m_JumpBob.DoBobCycle());
  95.                 PlayLandingSound();
  96.                 m_MoveDir.y = 0f;
  97.                 m_Jumping = false;
  98.             }
  99.             if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
  100.             {
  101.                 m_MoveDir.y = 0f;
  102.             }
  103.  
  104.             m_PreviouslyGrounded = m_CharacterController.isGrounded;
  105.         }
  106.  
  107.  
  108.         private void PlayLandingSound()
  109.         {
  110.             m_AudioSource.clip = m_LandSound;
  111.             m_AudioSource.Play();
  112.             m_NextStep = m_StepCycle + .5f;
  113.         }
  114.  
  115.  
  116.         private void FixedUpdate()
  117.         {
  118.             float speed;
  119.             GetInput(out speed);
  120.             // always move along the camera forward as it is the direction that it being aimed at
  121.             Vector3 desiredMove = Camera.main.transform.forward*m_Input.y + transform.right*m_Input.x;
  122.  
  123.             // get a normal for the surface that is being touched to move along it
  124.             RaycastHit hitInfo;
  125.             Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
  126.                                m_CharacterController.height/2f, ~0, QueryTriggerInteraction.Ignore);
  127.             desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
  128.  
  129.             m_MoveDir.x = desiredMove.x*speed;
  130.             m_MoveDir.z = desiredMove.z*speed;
  131.  
  132.  
  133.             if (m_CharacterController.isGrounded)
  134.             {
  135.                 m_MoveDir.y = -m_StickToGroundForce;
  136.  
  137.                 if (m_Jump)
  138.                 {
  139.                     m_MoveDir.y = m_JumpSpeed;
  140.                     PlayJumpSound();
  141.                     m_Jump = false;
  142.                     m_Jumping = true;
  143.                 }
  144.             }
  145.             else
  146.             {
  147.                 m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
  148.             }
  149.             m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
  150.  
  151.             ProgressStepCycle(speed);
  152.             UpdateCameraPosition(speed);
  153.  
  154.             //m_MouseLook.UpdateCursorLock();
  155.         }
  156.  
  157.  
  158.         private void PlayJumpSound()
  159.         {
  160.             m_AudioSource.clip = m_JumpSound;
  161.             m_AudioSource.Play();
  162.         }
  163.  
  164.  
  165.         private void ProgressStepCycle(float speed)
  166.         {
  167.             if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
  168.             {
  169.                 m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
  170.                              Time.fixedDeltaTime;
  171.             }
  172.  
  173.             if (!(m_StepCycle > m_NextStep))
  174.             {
  175.                 return;
  176.             }
  177.  
  178.             m_NextStep = m_StepCycle + m_StepInterval;
  179.  
  180.             PlayFootStepAudio();
  181.         }
  182.  
  183.  
  184.         private void PlayFootStepAudio()
  185.         {
  186.             if (!m_CharacterController.isGrounded)
  187.             {
  188.                 return;
  189.             }
  190.             // pick & play a random footstep sound from the array,
  191.             // excluding sound at index 0
  192.             int n = Random.Range(1, m_FootstepSounds.Length);
  193.             m_AudioSource.clip = m_FootstepSounds[n];
  194.             m_AudioSource.PlayOneShot(m_AudioSource.clip);
  195.             // move picked sound to index 0 so it's not picked next time
  196.             m_FootstepSounds[n] = m_FootstepSounds[0];
  197.             m_FootstepSounds[0] = m_AudioSource.clip;
  198.         }
  199.  
  200.  
  201.         private void UpdateCameraPosition(float speed)
  202.         {
  203.             Vector3 newCameraPosition;
  204.             if (!m_UseHeadBob)
  205.             {
  206.                 return;
  207.             }
  208.             if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
  209.             {
  210.                 m_Camera.transform.localPosition =
  211.                     m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
  212.                                       (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
  213.                 newCameraPosition = m_Camera.transform.localPosition;
  214.                 newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
  215.             }
  216.             else
  217.             {
  218.                 newCameraPosition = m_Camera.transform.localPosition;
  219.                 newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
  220.             }
  221.             m_Camera.transform.localPosition = newCameraPosition;
  222.         }
  223.  
  224.  
  225.         private void GetInput(out float speed)
  226.         {
  227.             // Read input
  228.             float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
  229.             float vertical = CrossPlatformInputManager.GetAxis("Vertical");
  230.  
  231.             if (bToggleWalk) {
  232.                 vertical = 1f;
  233.             }
  234.  
  235.             bool waswalking = m_IsWalking;
  236.  
  237. #if !MOBILE_INPUT
  238.             // On standalone builds, walk/run speed is modified by a key press.
  239.             // keep track of whether or not the character is walking or running
  240.             m_IsWalking = !Input.GetKey(KeyCode.LeftShift);
  241. #endif
  242.             // set the desired speed to be walking or running
  243.             speed = m_IsWalking ? m_WalkSpeed : m_RunSpeed;
  244.             m_Input = new Vector2(horizontal, vertical);
  245.  
  246.             // normalize input if it exceeds 1 in combined length:
  247.             if (m_Input.sqrMagnitude > 1)
  248.             {
  249.                 m_Input.Normalize();
  250.             }
  251.  
  252.             // handle speed change to give an fov kick
  253.             // only if the player is going to a run, is running and the fovkick is to be used
  254.             if (m_IsWalking != waswalking && m_UseFovKick && m_CharacterController.velocity.sqrMagnitude > 0)
  255.             {
  256.                 StopAllCoroutines();
  257.                 StartCoroutine(!m_IsWalking ? m_FovKick.FOVKickUp() : m_FovKick.FOVKickDown());
  258.             }
  259.         }
  260.  
  261.  
  262.         private void RotateView()
  263.         {
  264.             //m_MouseLook.LookRotation (transform, m_Camera.transform.parent);
  265.         }
  266.  
  267.  
  268.         private void OnControllerColliderHit(ControllerColliderHit hit)
  269.         {
  270.             Rigidbody body = hit.collider.attachedRigidbody;
  271.             //dont move the rigidbody if the character is on top of it
  272.             if (m_CollisionFlags == CollisionFlags.Below)
  273.             {
  274.                 return;
  275.             }
  276.  
  277.             if (body == null || body.isKinematic)
  278.             {
  279.                 return;
  280.             }
  281.             body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
  282.         }
  283.  
  284.         public void SetVerticalInput() {
  285.             Debug.Log("test");
  286.         }
  287.     }
  288. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement