- Home /
I am trying to create a leaning mechanic for an FPS game but I have one problem
Hello,
I have some code for a FPS leaning mechanic but there is one issue with it.
I am using the lerp function to smooth the leaning which is controlled by some bools. When the player leans in a direction, once they let go of the lean key, it is supposed to go into a 'recovery' phase before they can lean in the other direction. This is to smooth the camera back to the middle position so it doesn't just snap to the other side instantly.
This works but the issue is that if the player is leaned totally in one direction and then without releasing the lean key and they press the other lean key, it instantly snaps the camera to the opposite lean position without the 'recovery' bool being activated
Code:
public class CameraLean : MonoBehaviour
{
public Vector3 defaultRotation;
public Vector3 targetRotation;
public float leanAmount = 20f;
public Vector3 defaultPosition;
public Vector3 targetPosition;
public float offsetX = 0.35f;
public float offsetY = 0.05f;
public float speed;
public bool leaningRight;
public bool leaningLeft;
public bool recovering;
[Range(0,1)] public float perc;
// Start is called before the first frame update
void Start()
{
defaultPosition = new Vector3(0, 1.75f, 0);
defaultRotation = Vector3.zero;
}
// Update is called once per frame
void Update()
{
//Input
if (!recovering)
{
if (!leaningRight)
{
leaningLeft = Input.GetKey(KeyCode.Q);
}
if (!leaningLeft)
{
leaningRight = Input.GetKey(KeyCode.E);
}
}
//Leaning
if (leaningLeft && !recovering)
{
targetRotation = new Vector3(defaultRotation.x, defaultRotation.y, defaultRotation.z + leanAmount);
targetPosition = new Vector3(defaultPosition.x + -offsetX, defaultPosition.y - offsetY, defaultPosition.z);
perc += Time.deltaTime * speed;
perc = Mathf.Clamp(perc, 0, 1);
}
if (leaningRight && !recovering)
{
targetRotation = new Vector3(defaultRotation.x, defaultRotation.y, defaultRotation.z - leanAmount);
targetPosition = new Vector3(defaultPosition.x + offsetX, defaultPosition.y - offsetY, defaultPosition.z);
perc += Time.deltaTime * speed;
perc = Mathf.Clamp(perc, 0, 1);
}
//Not leaning
if (!leaningLeft && !leaningRight)
{
if (perc > 0)
{
recovering = true;
}
else
{
recovering = false;
}
perc -= Time.deltaTime * speed;
perc = Mathf.Clamp(perc, 0, 1);
}
//Apply Rotation
transform.localEulerAngles = Vector3.Lerp(defaultRotation, targetRotation, perc);
//Apply Translation
transform.localPosition = Vector3.Lerp(defaultPosition, targetPosition, perc);
}
}
Would anyone be able to explain why this is behaving like this? Or if there is a better solution?
Thanks
Answer by Captain_Pineapple · Dec 02, 2021 at 11:24 AM
Yes it makes perfect sense and yes there are better solutions.
Then main issue you have is that you use 3 flags (lean left, lean right, recovering) to track 2 states (where are we leaning currently, are we recovering)
By doing it this way you loose the information of "where you currently are leaning" once both left and right are set to true. And since if you are leaning without releasing the button your "recovery" is false you can simply press the other button which will then say: "hey - right is now pressed and we are not recovering so we should just change the target rotation to the other extreme position without resetting our lerp perc
value"
So how to fix this?
First up: If you have this many if-else statements for the same flags after each other: try to simplify that. The current version is really messy which makes it easy to miss a case. But in general i'd suggest that you introduce a state enumeration:
public enum LeanState { None, Left, Right };
public LeanState currentLeanState;
So with this you start in leanstate "None". Then each frame you check the Input for Q and E. If both or none are pressed go to state None. If only one is pressed go to the corresponding Left/Right state. In all cases whenever you change the state reset your lerp perc
variable and set the corresponding target rotation.
Then after that have a check for the current state and apply the new rotation as you already do.
Let me know if anything was unclear and if this solved your issue.
Your answer
Follow this Question
Related Questions
Vive Controller not enabled until mouse click 0 Answers
Multiple Cars not working 1 Answer
I need help with adding double jump to my player movement script 0 Answers
sensitivity slider/ drop down help 0 Answers
Distribute terrain in zones 3 Answers