- Home /
Question by
HarshPandey13 · Feb 28, 2018 at 11:42 AM ·
bobbingbob
Stop Head Bobbing when Player is trying to move in front of an obstacle or wall
In Case If You can't understand the question i am attaching URL to a video file to show my problem!
URL - https://youtu.be/drRxVkfQReM
I am using this code for head bobbing :-
public float bobSpeed = 1f;
public float maxHorizontalBob = 0.08f, maxHorizontalBobWhileRunning = 0.2f;
public float maxVerticalBob = 0.05f, maxVerticalBobWhileRunning = 0.1f;
public float eyeHeightRatio = 0.9f; // the ratio of the height of the cam to the height of the player
private Vector3 parentLastPostion; //the last position of the player
private float bobStepCounter = 0f;
void Awake()
{
parentLastPostion = transform.parent.position;
}
void Update()
{
#region Motion Bob
if (transform.parent.GetComponent<AFPS_PlayerMovement> ().isGrounded /*If the player on grounded*/ && transform.parent.GetComponent<Rigidbody> ().velocity.magnitude > 0) {
if (!transform.parent.GetComponent<AFPS_PlayerMovement> ().IsRunning) {
bobStepCounter += Vector3.Distance (parentLastPostion, transform.parent.position) * bobSpeed;
float posX, posY;
posX = Mathf.Sin (bobStepCounter) * maxHorizontalBob;
posY = (Mathf.Cos (bobStepCounter * 2) * maxVerticalBob * -1) + (transform.localScale.y * eyeHeightRatio) - (transform.localScale.y / 2);
transform.localPosition = new Vector3 (posX, posY, transform.localPosition.z);
parentLastPostion = transform.parent.position;
} else {
bobStepCounter += Vector3.Distance (parentLastPostion, transform.parent.position) * bobSpeed;
float posX, posY;
posX = Mathf.Sin (bobStepCounter) * maxHorizontalBobWhileRunning;
posY = (Mathf.Cos (bobStepCounter * 2) * maxVerticalBobWhileRunning * -1) + (transform.localScale.y * eyeHeightRatio) - (transform.localScale.y / 2);
transform.localPosition = new Vector3 (posX, posY, transform.localPosition.z);
parentLastPostion = transform.parent.position;
}
}
#endregion
}
Comment
Your answer
Follow this Question
Related Questions
Sample Assets FP bobbing 0 Answers
How to make a gun bob 1 Answer
Idle camera bob script? 1 Answer
How would I get camera bobbing? 0 Answers
Better way to make a Weapon Bob animation when walk 2 Answers