PlayerMoveOnSphere.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Cinemachine.Utility;
  2. using UnityEngine;
  3. namespace Cinemachine.Examples
  4. {
  5. public class PlayerMoveOnSphere : MonoBehaviour
  6. {
  7. public SphereCollider Sphere;
  8. public float speed = 5;
  9. public bool rotatePlayer = true;
  10. public float rotationDamping = 0.5f;
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. #if ENABLE_LEGACY_INPUT_MANAGER
  15. Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  16. if (input.magnitude > 0)
  17. {
  18. input = Camera.main.transform.rotation * input;
  19. if (input.magnitude > 0.001f)
  20. {
  21. transform.position += input * (speed * Time.deltaTime);
  22. if (rotatePlayer)
  23. {
  24. float t = Cinemachine.Utility.Damper.Damp(1, rotationDamping, Time.deltaTime);
  25. Quaternion newRotation = Quaternion.LookRotation(input.normalized, transform.up);
  26. transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, t);
  27. }
  28. }
  29. }
  30. // Stick to sphere surface
  31. if (Sphere != null)
  32. {
  33. var up = transform.position - Sphere.transform.position;
  34. up = up.normalized;
  35. var fwd = transform.forward.ProjectOntoPlane(up);
  36. transform.position = Sphere.transform.position + up * (Sphere.radius + transform.localScale.y / 2);
  37. transform.rotation = Quaternion.LookRotation(fwd, up);
  38. }
  39. #else
  40. InputSystemHelper.EnableBackendsWarningMessage();
  41. #endif
  42. }
  43. }
  44. }