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.

54 lines
1.9 KiB

  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.Utility
  4. {
  5. [Serializable]
  6. public class CurveControlledBob
  7. {
  8. public float HorizontalBobRange = 0.33f;
  9. public float VerticalBobRange = 0.33f;
  10. public AnimationCurve Bobcurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(0.5f, 1f),
  11. new Keyframe(1f, 0f), new Keyframe(1.5f, -1f),
  12. new Keyframe(2f, 0f)); // sin curve for head bob
  13. public float VerticaltoHorizontalRatio = 1f;
  14. private float m_CyclePositionX;
  15. private float m_CyclePositionY;
  16. private float m_BobBaseInterval;
  17. private Vector3 m_OriginalCameraPosition;
  18. private float m_Time;
  19. public void Setup(Camera camera, float bobBaseInterval)
  20. {
  21. m_BobBaseInterval = bobBaseInterval;
  22. m_OriginalCameraPosition = camera.transform.localPosition;
  23. // get the length of the curve in time
  24. m_Time = Bobcurve[Bobcurve.length - 1].time;
  25. }
  26. public Vector3 DoHeadBob(float speed)
  27. {
  28. float xPos = m_OriginalCameraPosition.x + (Bobcurve.Evaluate(m_CyclePositionX)*HorizontalBobRange);
  29. float yPos = m_OriginalCameraPosition.y + (Bobcurve.Evaluate(m_CyclePositionY)*VerticalBobRange);
  30. m_CyclePositionX += (speed*Time.deltaTime)/m_BobBaseInterval;
  31. m_CyclePositionY += ((speed*Time.deltaTime)/m_BobBaseInterval)*VerticaltoHorizontalRatio;
  32. if (m_CyclePositionX > m_Time)
  33. {
  34. m_CyclePositionX = m_CyclePositionX - m_Time;
  35. }
  36. if (m_CyclePositionY > m_Time)
  37. {
  38. m_CyclePositionY = m_CyclePositionY - m_Time;
  39. }
  40. return new Vector3(xPos, yPos, 0f);
  41. }
  42. }
  43. }