ObjectSpin.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TMPro.Examples
  4. {
  5. public class ObjectSpin : MonoBehaviour
  6. {
  7. #pragma warning disable 0414
  8. public float SpinSpeed = 5;
  9. public int RotationRange = 15;
  10. private Transform m_transform;
  11. private float m_time;
  12. private Vector3 m_prevPOS;
  13. private Vector3 m_initial_Rotation;
  14. private Vector3 m_initial_Position;
  15. private Color32 m_lightColor;
  16. private int frames = 0;
  17. public enum MotionType { Rotation, BackAndForth, Translation };
  18. public MotionType Motion;
  19. void Awake()
  20. {
  21. m_transform = transform;
  22. m_initial_Rotation = m_transform.rotation.eulerAngles;
  23. m_initial_Position = m_transform.position;
  24. Light light = GetComponent<Light>();
  25. m_lightColor = light != null ? light.color : Color.black;
  26. }
  27. // Update is called once per frame
  28. void Update()
  29. {
  30. if (Motion == MotionType.Rotation)
  31. {
  32. m_transform.Rotate(0, SpinSpeed * Time.deltaTime, 0);
  33. }
  34. else if (Motion == MotionType.BackAndForth)
  35. {
  36. m_time += SpinSpeed * Time.deltaTime;
  37. m_transform.rotation = Quaternion.Euler(m_initial_Rotation.x, Mathf.Sin(m_time) * RotationRange + m_initial_Rotation.y, m_initial_Rotation.z);
  38. }
  39. else
  40. {
  41. m_time += SpinSpeed * Time.deltaTime;
  42. float x = 15 * Mathf.Cos(m_time * .95f);
  43. float y = 10; // *Mathf.Sin(m_time * 1f) * Mathf.Cos(m_time * 1f);
  44. float z = 0f; // *Mathf.Sin(m_time * .9f);
  45. m_transform.position = m_initial_Position + new Vector3(x, z, y);
  46. // Drawing light patterns because they can be cool looking.
  47. //if (frames > 2)
  48. // Debug.DrawLine(m_transform.position, m_prevPOS, m_lightColor, 100f);
  49. m_prevPOS = m_transform.position;
  50. frames += 1;
  51. }
  52. }
  53. }
  54. }