First person shooter : Leaning!
I am a beginner with very limited knowledge when it comes to coding and scripting. I am currently working on a first person shooter and in it I wish to implement a "leaning system" similar to that found in the early Thief games.
Is there a simple script that I could add to the default FPSController that comes with Unity5 to achive this?
Here's a visual representation of what I mean (Sorry for the crap quality) : https://media.giphy.com/media/3o7ZeJogUUp3hhZpOU/giphy.gif
Again I would like to stress that I am beginner so please try and explain this to me in layman's terms ergo as simple as you possibly can.
Thanks.
Answer by Jessespike · Feb 04, 2016 at 08:29 PM
You can rotate the camera around a pivot to simulate a leaning effect. Assuming you're using the standard FPSController, add a pivot to the Controller and parent the camera to it. Like this:
FPSController
Pivot
FirstPersonController
Place the script on the camera (FirstPersonController), and set up the pivot in the inspector
public class LeanBehaviour : MonoBehaviour {
public Transform _Pivot;
public float speed = 100f;
public float maxAngle = 20f;
float curAngle = 0f;
// Use this for initialization
void Awake () {
if (_Pivot == null && transform.parent != null) _Pivot = transform.parent;
}
// Update is called once per frame
void Update () {
// lean left
if (Input.GetKey(KeyCode.Q))
{
curAngle = Mathf.MoveTowardsAngle(curAngle, maxAngle, speed * Time.deltaTime);
}
// lean right
else if (Input.GetKey(KeyCode.E))
{
curAngle = Mathf.MoveTowardsAngle(curAngle, -maxAngle, speed * Time.deltaTime);
}
// reset lean
else
{
curAngle = Mathf.MoveTowardsAngle(curAngle, 0f, speed * Time.deltaTime);
}
_Pivot.transform.localRotation = Quaternion.AngleAxis(curAngle, Vector3.forward);
}
}
Answer by leejewitt · Jun 28, 2021 at 05:31 PM
hi how would if get it to stop at the angle it hits a wall as i can lean straight through walls?
Your answer
Follow this Question
Related Questions
Jump Animation Script and colliders not working properly 1 Answer
How to increase the time between updates? 1 Answer
Animations Bad Scripting | Controller Script Buggy | Collider Problem | Beginner Here! 0 Answers
BasicFPS controller "Can not be loaded" 0 Answers
why the Timer isn't working 0 Answers