# Trouble coding a peeking system like in Rainbow Six Siege???

Hey guys im having trouble with the mathf.LerpAngles function in Unity. In my code i first ask for the input and establish the number of key presses it has been since the first “i” was equal to zero (even numbers mean that the player is peeking, while odd means the camera is at origin). For some reason my Camera will only rotate at the center. If a peeking button is pressed while not looking at the center, the camera snaps to it automatically. This is very incorrect. Also It doesnt seem to take into account the parameters i have set to see if i is even or odd. For now the positional statements are commented out because i am trying to fix lerping between angles first. Thank you all in advance! I appreciate any help or explanation i can get!

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerLean : MonoBehaviour {

``````private int i = 0;
private Vector3 currAngle;
private Vector3 tarAngle;

// Use this for initialization
void Start () {
Vector3 currAngle = transform.eulerAngles;
}

// Update is called once per frame
void Update () {

if (Input.GetButtonDown("Left Lean") && (i % 2) == 0)
{
tarAngle = new Vector3(currAngle.x, currAngle.y, 10f);

currAngle = new Vector3
(
currAngle.x,
currAngle.y,
Mathf.LerpAngle(currAngle.z, tarAngle.z, 2 * Time.fixedDeltaTime)
);

transform.eulerAngles = currAngle;

//        fpsCam.transform.position = new Vector3(Mathf.Lerp(fpsCam.transform.position.x, -0.39f, 0.5f * Time.fixedDeltaTime), 0f, 0f);

i += 1;

}

if (Input.GetButtonDown("Right Lean") && (i % 2) == 0)
{
tarAngle = new Vector3(currAngle.x, currAngle.y, -10f);

currAngle = new Vector3
(
currAngle.x,
currAngle.y,
Mathf.LerpAngle(currAngle.z, tarAngle.z, 2 * Time.fixedDeltaTime)
);

transform.eulerAngles = currAngle;

//        fpsCam.transform.position = new Vector3(Mathf.Lerp(fpsCam.transform.position.x, -0.39f, 0.5f * Time.fixedDeltaTime), 0f, 0f);

i += 1;
}

if ((Input.GetButtonDown("Left Lean") || Input.GetButtonDown("Right Lean")) && (i % 2) != 0)
{
tarAngle = new Vector3(currAngle.x, currAngle.y, 0f);

currAngle = new Vector3
(
currAngle.x,
currAngle.y,
Mathf.LerpAngle(currAngle.z, tarAngle.z, 2 * Time.fixedDeltaTime)
);

transform.eulerAngles = currAngle;

//        fpsCam.transform.position = new Vector3(Mathf.Lerp(fpsCam.transform.position.x, -0.39f, 0.5f * Time.fixedDeltaTime), 0f, 0f);

i += 1;
}
}
``````

}

using UnityEngine;

``````//RED NOTE

public class playerLean : MonoBehaviour {
/////////////////////////////////VAR/////////////////////////////////

// Use axis from Edit -> Project -> Input -> Axis ( create a new one and name "Lean" )
/*You should use the following consturct:
pivotY
|
+---+ pivotZ    - place script here
|
+--- pivotX ( camera, etc... )

pivot X should have the local height elevated, for actual leaning ( local height - y )
*/

//////////////////////////////// CFG
public float    leanAngle;          //Target lean angle

public float    leanDampingTo;     //Damp to peak angle
public float    leanDampingBack;   //Damp to angle zero

public float    leanError;         //Floating points are not quite as accurate as i would wish - set to 0.1

//////////////////////////////// MACHINE - DO NOT TOUCH =]]
public float    leanDampingDelta;   //Lean damping currently in use
public float    leanDelta;          //Lean distance remaining

public bool     leanStatus;         //Still in lean?        // -1 for left, 1 for right, 0 for home
public int      leanDirection;      //Lean direction

/////////////////////////////////API/////////////////////////////////
void Update() {
if( (int)Input.GetAxis("Lean") != -leanDirection ) {
leanDirection = -(int)Input.GetAxis("Lean");
leanStatus = true;
CheckDeltas();
}

if( leanStatus ) {
ApplyLean();
CheckLean();
}
}

/////////////////////////////////FCT/////////////////////////////////
void ApplyLean() {
transform.Rotate( 0, 0, leanDelta * leanDampingDelta , Space.Self );
leanDelta -= leanDelta * leanDampingDelta;
}

void CheckLean() {
if ( Mod( leanAngle * leanDirection - AngleFilter(transform.localRotation.eulerAngles.z) ) < leanError  ) {
Vector3 clamped = transform.localRotation.eulerAngles;
clamped.z = leanAngle * leanDirection;
transform.localRotation = Quaternion.Euler(clamped);
leanStatus = false;
}
}

void CheckDeltas() {
Vector3 tmp = transform.localRotation.eulerAngles;

leanDelta = leanAngle * leanDirection - AngleFilter(tmp.z);

if (leanDirection == 0) {
leanDampingDelta = leanDampingBack;
} else {
leanDampingDelta = leanDampingTo;
}
}

float AngleFilter( float a ) {
if ( a > 180 ) a -= 360;
return a;
}

float Mod( float a ) {
if (a < 0) return -a;
return a;
}

}
``````