edx/assignment10final/Assets/Standard Assets/Utility/CurveControlledBob.cs

55 lines
1.9 KiB
C#
Raw Normal View History

2022-11-10 21:56:29 -03:00
using System;
using UnityEngine;
namespace UnityStandardAssets.Utility
{
[Serializable]
public class CurveControlledBob
{
public float HorizontalBobRange = 0.33f;
public float VerticalBobRange = 0.33f;
public AnimationCurve Bobcurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(0.5f, 1f),
new Keyframe(1f, 0f), new Keyframe(1.5f, -1f),
new Keyframe(2f, 0f)); // sin curve for head bob
public float VerticaltoHorizontalRatio = 1f;
private float m_CyclePositionX;
private float m_CyclePositionY;
private float m_BobBaseInterval;
private Vector3 m_OriginalCameraPosition;
private float m_Time;
public void Setup(Camera camera, float bobBaseInterval)
{
m_BobBaseInterval = bobBaseInterval;
m_OriginalCameraPosition = camera.transform.localPosition;
// get the length of the curve in time
m_Time = Bobcurve[Bobcurve.length - 1].time;
}
public Vector3 DoHeadBob(float speed)
{
float xPos = m_OriginalCameraPosition.x + (Bobcurve.Evaluate(m_CyclePositionX)*HorizontalBobRange);
float yPos = m_OriginalCameraPosition.y + (Bobcurve.Evaluate(m_CyclePositionY)*VerticalBobRange);
m_CyclePositionX += (speed*Time.deltaTime)/m_BobBaseInterval;
m_CyclePositionY += ((speed*Time.deltaTime)/m_BobBaseInterval)*VerticaltoHorizontalRatio;
if (m_CyclePositionX > m_Time)
{
m_CyclePositionX = m_CyclePositionX - m_Time;
}
if (m_CyclePositionY > m_Time)
{
m_CyclePositionY = m_CyclePositionY - m_Time;
}
return new Vector3(xPos, yPos, 0f);
}
}
}