C# Lerping Problem
Hello all! I am fairly new to C# programming in general and am trying to learn it with Unity. I am using the default rigidbody fps controller prefab which has a headbob script attached to it by default. I noticed that while I was walking around my test map the camera position would reset (by teleporting to its original position) if I stopped walking in the middle of a bob. I figured out that if I take out a line (marked with many '>' symbols) that it would get rid of the head bob reset and make it look much smoother. It looks very funny, though, when the camera is in a different position every time I stop walking. Is there something that I need to add somewhere that would smoothly reset the camera to its original position (preferably with lerping)? Please help. Thanks! Here is my code:
using System;
using UnityEngine;
using UnityStandardAssets.Utility;
namespace UnityStandardAssets.Characters.FirstPerson
{
public class HeadBob : MonoBehaviour
{
public Camera Camera;
public CurveControlledBob motionBob = new CurveControlledBob();
public LerpControlledBob jumpAndLandingBob = new LerpControlledBob();
public RigidbodyFirstPersonController rigidbodyFirstPersonController;
public float StrideInterval;
[Range(0f, 1f)] public float RunningStrideLengthen;
// private CameraRefocus m_CameraRefocus;
private bool m_PreviouslyGrounded;
private Vector3 m_OriginalCameraPosition;
private void Start()
{
motionBob.Setup(Camera, StrideInterval);
m_OriginalCameraPosition = Camera.transform.localPosition;
// m_CameraRefocus = new CameraRefocus(Camera, transform.root.transform, Camera.transform.localPosition);
}
private void Update()
{
// m_CameraRefocus.GetFocusPoint();
Vector3 newCameraPosition;
if (rigidbodyFirstPersonController.Velocity.magnitude > 0 && rigidbodyFirstPersonController.Grounded)
{
Camera.transform.localPosition = motionBob.DoHeadBob(rigidbodyFirstPersonController.Velocity.magnitude*(rigidbodyFirstPersonController.Running ? RunningStrideLengthen : 1f));
newCameraPosition = Camera.transform.localPosition;
newCameraPosition.y = Camera.transform.localPosition.y - jumpAndLandingBob.Offset();
}
else
{
newCameraPosition = Camera.transform.localPosition;
>>>>>>>>>>>>>>> newCameraPosition.y = m_OriginalCameraPosition.y - jumpAndLandingBob.Offset();
}
Camera.transform.localPosition = newCameraPosition;
if (!m_PreviouslyGrounded && rigidbodyFirstPersonController.Grounded)
{
StartCoroutine(jumpAndLandingBob.DoBobCycle());
}
m_PreviouslyGrounded = rigidbodyFirstPersonController.Grounded;
// m_CameraRefocus.SetFocusPoint();
}
}
}
Your answer
Follow this Question
Related Questions
How To Reset Prefab Values after exiting Playmode? 0 Answers
How to make my player move forward when on an object 1 Answer
I'm having issues with collision and Physics 1 Answer
Player moves indefinitely after collision 0 Answers
How can I attach a Gameobject from the scene to a prefab on the Assets folder? 0 Answers