You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

63 lines
1.7KB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace coa4u
  5. {
  6. /// <summary>
  7. /// Rotates to the given euler angles.
  8. /// </summary>
  9. class ActionRotateTo : ActionInterval
  10. {
  11. protected Vector3 value;
  12. protected Vector3 path;
  13. public ActionRotateTo(Vector3 targetValue, float targetDuration)
  14. : base(targetDuration)
  15. {
  16. value = targetValue;
  17. }
  18. public ActionRotateTo(float angle, float targetDuration)
  19. : this(new Vector3(0, 0, angle), targetDuration)
  20. {
  21. locks = Axises.rxy;
  22. }
  23. public override ActionInstant Clone()
  24. {
  25. return new ActionRotateTo(value, duration);
  26. }
  27. public override void Start()
  28. {
  29. base.Start();
  30. path = new Vector3();
  31. for (int i = 0; i < 3; i++)
  32. {
  33. float t = value[i];
  34. float f = transform.rotation.eulerAngles[i];
  35. if ((f - t) <= 180 && (f - t) >= -180)
  36. {
  37. path[i] = t - f;
  38. }
  39. else if ((f - t) > 180)
  40. {
  41. path[i] = t - f + 360;
  42. }
  43. else if ((f - t) < -180)
  44. {
  45. path[i] = t - f - 360;
  46. }
  47. }
  48. if (locks != Axises.none)
  49. LockAxises(ref path);
  50. }
  51. public override void Step(float dt)
  52. {
  53. float d = dt / duration;
  54. Vector3 rotation = path * d;
  55. transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + rotation);
  56. }
  57. }
  58. }