PlayerMovePhysics.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using UnityEngine;
  3. namespace Cinemachine.Examples
  4. {
  5. public class PlayerMovePhysics : MonoBehaviour
  6. {
  7. public float speed = 5;
  8. public bool worldDirection = true;
  9. public bool rotatePlayer = true;
  10. public Action spaceAction;
  11. public Action enterAction;
  12. Rigidbody rb;
  13. void Start()
  14. {
  15. rb = GetComponent<Rigidbody>();
  16. }
  17. private void OnEnable()
  18. {
  19. transform.position += new Vector3(10, 0, 0);
  20. }
  21. void FixedUpdate()
  22. {
  23. #if ENABLE_LEGACY_INPUT_MANAGER
  24. Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  25. //input = Vector3.forward;
  26. if (input.magnitude > 0)
  27. {
  28. Vector3 fwd = worldDirection
  29. ? Vector3.forward
  30. : transform.position - Camera.main.transform.position;
  31. fwd.y = 0;
  32. fwd = fwd.normalized;
  33. if (fwd.magnitude > 0.001f)
  34. {
  35. Quaternion inputFrame = Quaternion.LookRotation(fwd, Vector3.up);
  36. input = inputFrame * input;
  37. if (input.magnitude > 0.001f)
  38. {
  39. rb.AddForce(speed * input);
  40. if (rotatePlayer)
  41. transform.rotation = Quaternion.LookRotation(input.normalized, Vector3.up);
  42. }
  43. }
  44. }
  45. if (Input.GetKeyDown(KeyCode.Space) && spaceAction != null)
  46. spaceAction();
  47. if (Input.GetKeyDown(KeyCode.Return) && enterAction != null)
  48. enterAction();
  49. #else
  50. InputSystemHelper.EnableBackendsWarningMessage();
  51. #endif
  52. }
  53. }
  54. }