Character.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.AI;
  5. #pragma warning disable CS0649
  6. namespace EPOOutline.Demo
  7. {
  8. public class Character : MonoBehaviour
  9. {
  10. [SerializeField]
  11. private AudioSource walkSource;
  12. [SerializeField]
  13. private NavMeshAgent agent;
  14. [SerializeField]
  15. private Animator characterAnimator;
  16. private float initialWalkVolume = 0.0f;
  17. private Camera mainCamera;
  18. private void Start()
  19. {
  20. initialWalkVolume = walkSource.volume;
  21. mainCamera = Camera.main;
  22. agent.updateRotation = false;
  23. }
  24. private void Update()
  25. {
  26. var forward = mainCamera.transform.forward;
  27. forward.y = 0;
  28. forward.Normalize();
  29. var right = mainCamera.transform.right;
  30. right.y = 0;
  31. right.Normalize();
  32. var direction = forward * Input.GetAxis("Vertical") + right * Input.GetAxis("Horizontal");
  33. if (direction.magnitude > 0.1f)
  34. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(direction), Time.deltaTime * agent.angularSpeed);
  35. agent.velocity = direction.normalized * agent.speed;
  36. walkSource.volume = initialWalkVolume * (agent.velocity.magnitude / agent.speed);
  37. characterAnimator.SetBool("IsRunning", direction.magnitude > 0.1f);
  38. }
  39. private void OnTriggerEnter(Collider other)
  40. {
  41. var collectable = other.GetComponent<ICollectable>();
  42. if (collectable == null)
  43. return;
  44. collectable.Collect(gameObject);
  45. }
  46. }
  47. }