daily pastebin goal
63%
SHARE
TWEET

Firstpersoncontroller.cs

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