Advertisement
GamerRO

Untitled

Aug 19th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityStandardAssets.Utility;
  5.  
  6. [RequireComponent(typeof(AudioSource))]
  7. [RequireComponent(typeof(Rigidbody))]
  8. [RequireComponent(typeof(CharacterController))]
  9. public class PlayerMotor : MonoBehaviour {
  10.  
  11. [SerializeField]
  12. private Camera cam;
  13.  
  14. [SerializeField] private CharacterController m_CharacterController;
  15.  
  16. private Vector3 velocity = Vector3.zero;
  17. private Vector3 rotation = Vector3.zero;
  18. private Vector3 jumpvelocity = Vector3.zero;
  19. private float cameraRotationX = 0f;
  20. private float currentCameraRotationX = 0f;
  21.  
  22. [SerializeField] private AudioClip[] FootstepSounds; // an array of footstep sounds that will be randomly selected from.
  23.  
  24. [SerializeField] private AudioSource AS;
  25.  
  26. [SerializeField] private AudioClip JumpSound; // the sound played when character leaves the ground.
  27. [SerializeField] private AudioClip LandSound; // the sound played when character touches back on ground.
  28.  
  29. private Rigidbody rb;
  30.  
  31. [SerializeField]
  32. private float cameraRotationLimit = 90f;
  33.  
  34. public GameObject PlBody;
  35.  
  36. private void PlayFootStepAudio()
  37. {
  38. if (!m_CharacterController.isGrounded)
  39. {
  40. return;
  41. }
  42. // pick & play a random footstep sound from the array,
  43. // excluding sound at index 0
  44. int n = Random.Range(1, FootstepSounds.Length);
  45. AS.clip = FootstepSounds[n];
  46. AS.PlayOneShot(AS.clip);
  47. // move picked sound to index 0 so it's not picked next time
  48. FootstepSounds[n] = FootstepSounds[0];
  49. FootstepSounds[0] = AS.clip;
  50. }
  51.  
  52. public void PlayLandingSound()
  53. {
  54. AS.clip = LandSound;
  55. AS.Play();
  56. }
  57.  
  58. public void PlayJumpSound()
  59. {
  60. AS.clip = JumpSound;
  61. AS.Play();
  62. }
  63.  
  64. void Start()
  65. {
  66. rb = GetComponent<Rigidbody>();
  67. AS = GetComponent<AudioSource>();
  68. m_CharacterController = transform.GetComponent<CharacterController>();
  69. }
  70.  
  71. public void Move(Vector3 _velocity)
  72. {
  73. velocity = _velocity;
  74. }
  75.  
  76. public void Rotate(Vector3 _rotation)
  77. {
  78. rotation = _rotation;
  79. }
  80.  
  81. public void Jump(Vector3 _jumpvelocity)
  82. {
  83. jumpvelocity = _jumpvelocity;
  84. }
  85.  
  86. // Gets a rotational vector for the camera
  87. public void RotateCamera(float _cameraRotationX)
  88. {
  89. cameraRotationX = _cameraRotationX;
  90. }
  91.  
  92. void FixedUpdate()
  93. {
  94. PerformMovement();
  95. PerformRotation();
  96. PerformJumping();
  97. }
  98.  
  99. void PerformMovement()
  100. {
  101. if (velocity != Vector3.zero)
  102. {
  103. rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
  104. PlayFootStepAudio();
  105. }
  106. }
  107.  
  108. //Perform rotation
  109. void PerformRotation()
  110. {
  111. rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
  112. if (cam != null)
  113. {
  114. // Set our rotation and clamp it
  115. currentCameraRotationX -= cameraRotationX;
  116. currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -cameraRotationLimit, cameraRotationLimit);
  117.  
  118. //Apply our rotation to the transform of our camera
  119. cam.transform.localEulerAngles = new Vector3(currentCameraRotationX, 0f, 0f);
  120. }
  121. }
  122.  
  123. public void PerformJumping()
  124. {
  125. if (jumpvelocity != Vector3.zero)
  126. {
  127. rb.MovePosition(rb.position + jumpvelocity * Time.fixedDeltaTime);
  128. }
  129. }
  130.  
  131. public void Update()
  132. {
  133. if (m_CharacterController.isGrounded == true)
  134. {
  135. jumpvelocity = Vector3.zero;
  136. }
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement