Skip to content

Instantly share code, notes, and snippets.

@KarlRamstedt
Created January 8, 2020 10:17
Show Gist options
  • Save KarlRamstedt/407d50725c7b6abeaf43aee802fdd88e to your computer and use it in GitHub Desktop.
Save KarlRamstedt/407d50725c7b6abeaf43aee802fdd88e to your computer and use it in GitHub Desktop.
A simple First Person Camera rotation script for Unity.
using UnityEngine;
/// <summary>
/// A simple FPP (First Person Perspective) camera rotation script.
/// Like those found in most FPS (First Person Shooter) games.
/// </summary>
public class FirstPersonCameraRotation : MonoBehaviour {
public float Sensitivity {
get { return sensitivity; }
set { sensitivity = value; }
}
[Range(0.1f, 9f)][SerializeField] float sensitivity = 2f;
[Tooltip("Limits vertical camera rotation. Prevents the flipping that happens when rotation goes above 90.")]
[Range(0f, 90f)][SerializeField] float yRotationLimit = 88f;
Vector2 rotation = Vector2.zero;
const string xAxis = "Mouse X"; //Strings in direct code generate garbage, storing and re-using them creates no garbage
const string yAxis = "Mouse Y";
void Update(){
rotation.x += Input.GetAxis(xAxis) * sensitivity;
rotation.y += Input.GetAxis(yAxis) * sensitivity;
rotation.y = Mathf.Clamp(rotation.y, -yRotationLimit, yRotationLimit);
var xQuat = Quaternion.AngleAxis(rotation.x, Vector3.up);
var yQuat = Quaternion.AngleAxis(rotation.y, Vector3.left);
transform.localRotation = xQuat * yQuat; //Quaternions seem to rotate more consistently than EulerAngles. Sensitivity seemed to change slightly at certain degrees using Euler. transform.localEulerAngles = new Vector3(-rotation.y, rotation.x, 0);
}
}
@JBusterJ
Copy link

JBusterJ commented Nov 9, 2021

When i turn to the opposite side the WS gets reversed. SO when i press W i go backwards when i turn the opposite side. pls help

@PandyShare
Are you using Vector3.forward or Transform.forward for your movement? If you're using Vector3.forward, I think you should use Transform.forward so that instead of moving forward relative to the world, you move forward relative to the player.
If you need more help, add me on discord @ JBuster#3191

@Space-Banane
Copy link

Yoink

@Coder-Boiiiiiii
Copy link

Coder-Boiiiiiii commented Apr 1, 2022

Ctrl+C and Ctrl+V

Done with camera

@Saurn98
Copy link

Saurn98 commented Jun 3, 2022

Ctrl+C Ctrl+V
I am done with my player movement and camera I am such a great programmer

@Aqueuse
Copy link

Aqueuse commented Jun 20, 2022

You basically saved my day ! Thank you ❤ !

@StarKeeper11
Copy link

Thank you so much
I was making a flying camera for my game, and I had a problem where the z orientation kept changing. THIS FIXED IT! Again, thank you.

@andboss25
Copy link

How can i make the camera folow the player?

@CoolGuy6772
Copy link

How can i make the camera folow the player?

Create a camera and make it the player's child.

@crsl110
Copy link

crsl110 commented Oct 16, 2022

Thank you so much
I had a issue with clamping and I managed to fix it after seeing your script

@RubeySchulz
Copy link

Awesome

@PineapplePeace
Copy link

Idk why, but I have gotten complier errors every time I try to run the script. Idk why this is since I have double checked the code and re written it several times... help pls

@Woutjeee
Copy link

@PineapplePeace What is the error that u got?

@InsaneGds
Copy link

Placed the script in Scripts folder, it still doesn't work. I can't move the player or the camera.

@KarlRamstedt
Copy link
Author

@GeoSubZero have you tried attaching it to the camera object?

@InsaneGds
Copy link

@KarlRamstedt Yes but it still doesn't works. Only the camera moves, the player is still static.

@KarlRamstedt
Copy link
Author

@GeoSubZero ... yes, that's how it's supposed to work. This script is camera rotation only. As indicated by the name.
You'll need to find/make a character controller for movement.

@Romaleks360
Copy link

literal strings don't create garbage, they are already interned by the compiler
https://stackoverflow.com/questions/42413654/is-there-any-performance-increase-by-changing-this-net-string-to-a-const-does

that's some extreme premature optimization, never do that.

@KarlRamstedt
Copy link
Author

@Romaleks360 Loads of people around the web recommend caching strings in Unity optimization efforts. Check around.
Might be something more specific to the Unity Engine rather than C#.

@andewx
Copy link

andewx commented Feb 15, 2024

Thank you so much I was making a flying camera for my game, and I had a problem where the z orientation kept changing. THIS FIXED IT! Again, thank you.

It pains me to say me too, not sure why the Quaternion.Euler() behaves that way.

@andewx
Copy link

andewx commented Feb 15, 2024

Karl can you explain why perhaps the Quaternion.Euler() rotation creates off-axis rolls?

@ItsMabb
Copy link

ItsMabb commented Mar 22, 2024

Thank you so much. Ive been struggling on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment