Headbob in C#
How can I make my FPS Character headbob? I tried everything but I cannot get my character to headbob
[CODE]
using System.Collections; using System.Collections.Generic; using System.Linq.Expressions; using UnityEngine;
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);
}
} }
[/CODE]
Answer by streeetwalker · Mar 26, 2020 at 09:47 AM
@thatguylogibusiness, you have the code to bob, but are calling it as a part of an animation loop or coroutine? It won't happen if you don't do that.
Your answer
Follow this Question
Related Questions
How to change scripting runtime to 4.7.2 in Unity 2019.1.0f2? It is by default targeting 4.7.1 0 Answers
First person shooter : Leaning! 2 Answers
Implementing an animation command in a commands queue system 1 Answer
example of new way to load levels 1 Answer
"The associated script cannot be loaded" 0 Answers