Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityStandardAssets.Utility;
- [RequireComponent(typeof(AudioSource))]
- [RequireComponent(typeof(Rigidbody))]
- [RequireComponent(typeof(CharacterController))]
- public class PlayerMotor : MonoBehaviour {
- [SerializeField]
- private Camera cam;
- [SerializeField] private CharacterController m_CharacterController;
- private Vector3 velocity = Vector3.zero;
- private Vector3 rotation = Vector3.zero;
- private Vector3 jumpvelocity = Vector3.zero;
- private float cameraRotationX = 0f;
- private float currentCameraRotationX = 0f;
- [SerializeField] private AudioClip[] FootstepSounds; // an array of footstep sounds that will be randomly selected from.
- [SerializeField] private AudioSource AS;
- [SerializeField] private AudioClip JumpSound; // the sound played when character leaves the ground.
- [SerializeField] private AudioClip LandSound; // the sound played when character touches back on ground.
- private Rigidbody rb;
- [SerializeField]
- private float cameraRotationLimit = 90f;
- public GameObject PlBody;
- private void PlayFootStepAudio()
- {
- if (!m_CharacterController.isGrounded)
- {
- return;
- }
- // pick & play a random footstep sound from the array,
- // excluding sound at index 0
- int n = Random.Range(1, FootstepSounds.Length);
- AS.clip = FootstepSounds[n];
- AS.PlayOneShot(AS.clip);
- // move picked sound to index 0 so it's not picked next time
- FootstepSounds[n] = FootstepSounds[0];
- FootstepSounds[0] = AS.clip;
- }
- public void PlayLandingSound()
- {
- AS.clip = LandSound;
- AS.Play();
- }
- public void PlayJumpSound()
- {
- AS.clip = JumpSound;
- AS.Play();
- }
- void Start()
- {
- rb = GetComponent<Rigidbody>();
- AS = GetComponent<AudioSource>();
- m_CharacterController = transform.GetComponent<CharacterController>();
- }
- public void Move(Vector3 _velocity)
- {
- velocity = _velocity;
- }
- public void Rotate(Vector3 _rotation)
- {
- rotation = _rotation;
- }
- public void Jump(Vector3 _jumpvelocity)
- {
- jumpvelocity = _jumpvelocity;
- }
- // Gets a rotational vector for the camera
- public void RotateCamera(float _cameraRotationX)
- {
- cameraRotationX = _cameraRotationX;
- }
- void FixedUpdate()
- {
- PerformMovement();
- PerformRotation();
- PerformJumping();
- }
- void PerformMovement()
- {
- if (velocity != Vector3.zero)
- {
- rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
- PlayFootStepAudio();
- }
- }
- //Perform rotation
- void PerformRotation()
- {
- rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
- if (cam != null)
- {
- // Set our rotation and clamp it
- currentCameraRotationX -= cameraRotationX;
- currentCameraRotationX = Mathf.Clamp(currentCameraRotationX, -cameraRotationLimit, cameraRotationLimit);
- //Apply our rotation to the transform of our camera
- cam.transform.localEulerAngles = new Vector3(currentCameraRotationX, 0f, 0f);
- }
- }
- public void PerformJumping()
- {
- if (jumpvelocity != Vector3.zero)
- {
- rb.MovePosition(rb.position + jumpvelocity * Time.fixedDeltaTime);
- }
- }
- public void Update()
- {
- if (m_CharacterController.isGrounded == true)
- {
- jumpvelocity = Vector3.zero;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement