- Home /
Camera Lean
I have made a script that is intended to make the camera lean using controls and effects similar to amnesia. Although when I attach the script to a camera it has no errors in the console but pressing q and e have no effect. Please help!! Thanks guys
Code:
using UnityEngine;
using System.Collections;
public class Lean : MonoBehaviour {
public float leanAngle = 35.0f;
public float leanSpeed = 5.0f;
public float leanBackSpeed = 6.0f;
void Update (){
if (Input.GetKey(KeyCode.Q)) {
LeanLeft();
}
else if (Input.GetKey(KeyCode.E)) {
LeanRight();
}
else
{
LeanBack();
}
}
void LeanLeft (){
// current Z-rotation
float currAngle = transform.rotation.eulerAngles.z;
//Quaternion rot = transform.rotation;
// target Z-rotation
float targetAngle = leanAngle;
if ( currAngle > 180.0f )
{
//targetAngle = 0.0f - leanAngle;
currAngle = 360 - currAngle;
}
//lerp value from current to target
float angle = Mathf.Lerp( currAngle, targetAngle, leanSpeed * Time.deltaTime );
//Debug.Log ( "Left : currAngle " + currAngle + " : targetAngle " + targetAngle + " : angle " + angle );
// rotate char
Quaternion rotAngle = Quaternion.Euler( transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, angle );
transform.rotation = rotAngle;
}
void LeanRight (){
// current Z-rotation
float currAngle = transform.rotation.eulerAngles.z;
// target Z-rotation
float targetAngle = leanAngle - 360.0f;
if ( currAngle > 180.0f )
{
targetAngle = 360.0f - leanAngle;
}
//lerp value from current to target
float angle = Mathf.Lerp( currAngle, targetAngle, leanSpeed * Time.deltaTime );
//Debug.Log ( "Right : currAngle " + currAngle + " : targetAngle " + targetAngle + " : angle " + angle );
// rotate char
Quaternion rotAngle = Quaternion.Euler( transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, angle );
transform.rotation = rotAngle;
}
void LeanBack (){
// current Z-rotation
float currAngle = transform.rotation.eulerAngles.z;
// target Z-rotation
float targetAngle = 0.0f;
if ( currAngle > 180.0f )
{
targetAngle = 360.0f;
}
//lerp value from current to target
float angle = Mathf.Lerp( currAngle, targetAngle, leanBackSpeed * Time.deltaTime );
//Debug.Log ( "Center : currAngle " + currAngle + " : targetAngle " + targetAngle + " : angle " + angle );
Quaternion rotAngle = Quaternion.Euler( transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, angle );
transform.rotation = rotAngle;
}
[RPC]
void Test (){}
}
@alucardj Its always odd seeing your own scripts come back to you, complete with your own comments and idiosyncratic member names. I think some people forget how small the internet community of Unity developers really is.
@gripingberry I suggest you learn to write a script first, before copying from the internet. The code is fine, its probably a set up error in the inspector. Some things to check:
Is the script actually attached to the GameObject
Are the values for the public fields in the inspector non zero
Is the component enabled?
Your answer
![](https://koobas.hobune.stream/wayback/20220613175542im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Weird FPS Camera 0 Answers
I cannot see button on camera preview 1 Answer
Implement a "FreeLook view" 1 Answer