- Home /
Lean main camera depending on input Axis (Horizontal)
I'm trying to do a simple leaning function for my character. Is an FPS game, what I'm trying to achieve is a small leaning of the main camera when moving on the horizontal axis to simulate the head movement beign dragged. The script kinda works but it doesn't take in account the negative value of the axis iven, so it only lean in one direction.
NOTE: The leaning is achieved by just rotating the main camera z euler angle.
Here is the script:
using UnityEngine;
[AddComponentMenu("Player Scripts/Player Head Track")]
public class PlayerHeadTrack : MonoBehaviour {
public GameObject mainCam;
public float leanAngle = 5f;
public float leanSpeed = 10f;
float curAngle;
float targetAngle;
float angle;
Quaternion rotAngle;
// Lean main camera when moving horizontally
public void LeanCamera (float axis) {
curAngle = mainCam.transform.localEulerAngles.z;
targetAngle = leanAngle - axis;
if ( axis == 0.0f ) targetAngle = 0.0f;
angle = Mathf.Lerp( curAngle, targetAngle, leanSpeed * Time.deltaTime );
rotAngle = Quaternion.Euler( mainCam.transform.localEulerAngles.x, mainCam.transform.localEulerAngles.y, angle );
mainCam.transform.localRotation = rotAngle;
}
}
To calculate the angle from my player controller script I just call the function LeanCamera this way:
When player is moving
playerHt.LeanCamera(Input.GetAxis("Horizontal"));
When doing nothing
playerHt.LeanCamera(0.0f);
How can I get both positive/negative values of the horizontal axis to work? I know they should be there no matter what, maybe is a problem on the LeanCamera script itself? Thanks in advance.
I'm confused with your code, but what is actually doing and what you want it to do. Why are you calling LeanCamera() with the axix? Do you want it to lean more/faster if the movement is fast, or something else? If you are doing what I think you are trying to do, maybe this line:
targetAngle = leanAngle - axis;
Should be something like:
targetAngle = axis * leanAngle;
If you explain more, I might have some better ways for you to lean.
I don't want to add to the angle, I want to only lean toward a direction depending where the player is moving, only on the horizontal axis, so for example you strafe right the camera lean toward right with a fixed angle: http://www.youtube.com/watch?v=$$anonymous$$4On9P_2__c something like Quake.
$$anonymous$$aybe the problem here is another I guess Euler angle are only from 0 to 360 and doesn't have a negative angle?
Answer by Kleptomaniac · Mar 09, 2013 at 12:40 PM
Hey,
Having read your last comment, I believe I understand what you want now.
Try using Quaternion.Lerp:
float maxRot = 45.0f; //Max neg and pos strafe rotation value
float rate = 2.0f; //Rate of change for Lerp
mainCam.transform.rotation = Quaternion.Lerp(mainCam.transform.rotation, Quaternion.Euler(axis * maxRot, mainCam.transform.rotation.y, mainCam.transform.rotation.z), Time.deltaTime * rate);
Hopefully that should work. Haven't tested though. :)
Klep
Thats it, it works, only things i had to change were the rotation to localRotation and the axis calculation to the Z of the euler. Thank you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093845im_/https://answers.unity.com/themes/thub/images/avi.jpg)