fps camera unity

public float turnSpeed = 4.0f;
public float moveSpeed = 2.0f;
 
public float minTurnAngle = -90.0f;
public float maxTurnAngle = 90.0f;
private float rotX;
 
void Update ()
{
    MouseAiming();
    KeyboardMovement();
}
 
void MouseAiming ()
{
    // get the mouse inputs
    float y = Input.GetAxis("Mouse X") * turnSpeed;
    rotX += Input.GetAxis("Mouse Y") * turnSpeed;
 
    // clamp the vertical rotation
    rotX = Mathf.Clamp(rotX, minTurnAngle, maxTurnAngle);
 
    // rotate the camera
    transform.eulerAngles = new Vector3(-rotX, transform.eulerAngles.y + y, 0);
}
 
void KeyboardMovement ()
{
    Vector3 dir = new Vector3(0, 0, 0);
 
    dir.x = Input.GetAxis("Horizontal");
    dir.z = Input.GetAxis("Vertical");
 
    transform.Translate(dir * moveSpeed * Time.deltaTime);
}

4
2
Redpenner 115 points

                                    using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
[AddComponentMenu("Camera-Control/Smooth Mouse Look")]
public class SmoothMouseLook : MonoBehaviour {
 
	public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
	public RotationAxes axes = RotationAxes.MouseXAndY;
	public float sensitivityX = 15F;
	public float sensitivityY = 15F;
 
	public float minimumX = -360F;
	public float maximumX = 360F;
 
	public float minimumY = -60F;
	public float maximumY = 60F;
 
	float rotationX = 0F;
	float rotationY = 0F;
 
	private List<float> rotArrayX = new List<float>();
	float rotAverageX = 0F;	
 
	private List<float> rotArrayY = new List<float>();
	float rotAverageY = 0F;
 
	public float frameCounter = 20;
 
	Quaternion originalRotation;
 
	void Update ()
	{
		if (axes == RotationAxes.MouseXAndY)
		{			
			rotAverageY = 0f;
			rotAverageX = 0f;
 
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
 
			rotArrayY.Add(rotationY);
			rotArrayX.Add(rotationX);
 
			if (rotArrayY.Count >= frameCounter) {
				rotArrayY.RemoveAt(0);
			}
			if (rotArrayX.Count >= frameCounter) {
				rotArrayX.RemoveAt(0);
			}
 
			for(int j = 0; j < rotArrayY.Count; j++) {
				rotAverageY += rotArrayY[j];
			}
			for(int i = 0; i < rotArrayX.Count; i++) {
				rotAverageX += rotArrayX[i];
			}
 
			rotAverageY /= rotArrayY.Count;
			rotAverageX /= rotArrayX.Count;
 
			rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);
			rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);
 
			Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
			Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
 
			transform.localRotation = originalRotation * xQuaternion * yQuaternion;
		}
		else if (axes == RotationAxes.MouseX)
		{			
			rotAverageX = 0f;
 
			rotationX += Input.GetAxis("Mouse X") * sensitivityX;
 
			rotArrayX.Add(rotationX);
 
			if (rotArrayX.Count >= frameCounter) {
				rotArrayX.RemoveAt(0);
			}
			for(int i = 0; i < rotArrayX.Count; i++) {
				rotAverageX += rotArrayX[i];
			}
			rotAverageX /= rotArrayX.Count;
 
			rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);
 
			Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
			transform.localRotation = originalRotation * xQuaternion;			
		}
		else
		{			
			rotAverageY = 0f;
 
			rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
 
			rotArrayY.Add(rotationY);
 
			if (rotArrayY.Count >= frameCounter) {
				rotArrayY.RemoveAt(0);
			}
			for(int j = 0; j < rotArrayY.Count; j++) {
				rotAverageY += rotArrayY[j];
			}
			rotAverageY /= rotArrayY.Count;
 
			rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);
 
			Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
			transform.localRotation = originalRotation * yQuaternion;
		}
	}
 
	void Start ()
	{		
                Rigidbody rb = GetComponent<Rigidbody>();	
		if (rb)
			rb.freezeRotation = true;
		originalRotation = transform.localRotation;
	}
 
	public static float ClampAngle (float angle, float min, float max)
	{
		angle = angle % 360;
		if ((angle >= -360F) && (angle <= 360F)) {
			if (angle < -360F) {
				angle += 360F;
			}
			if (angle > 360F) {
				angle -= 360F;
			}			
		}
		return Mathf.Clamp (angle, min, max);
	}
}

4 (2 Votes)
0
5
1

                                    //Fixed the issues with the previous controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraLook : MonoBehaviour
{
	public float minX = -60f;
	public float maxX = 60f;

	public float sensitivity;
	public Camera cam;

	float rotY = 0f;
	float rotX = 0f;

	void Start()
	{
		Cursor.lockState = CursorLockMode.Locked;
		Cursor.visible = false;
	}

    void Update()
    {
		rotY += Input.GetAxis("Mouse X") * sensitivity;
		rotX += Input.GetAxis("Mouse Y") * sensitivity;

		rotX = Mathf.Clamp(rotX, minX, maxX);

		transform.localEulerAngles = new Vector3(0, rotY, 0);
		cam.transform.localEulerAngles = new Vector3(-rotX, 0, 0);

		if (Input.GetKeyDown(KeyCode.Escape))
		{
        	//Mistake happened here vvvv
			Cursor.lockState = CursorLockMode.None;
			Cursor.visible = true;
		}

		if (Cursor.visible && Input.GetMouseButtonDown(1))
		{
			Cursor.lockState = CursorLockMode.Locked;
			Cursor.visible = false;
		}
	}
}

5 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
unity fps camera code unity camera fov for fps how to make fps camera control in unity unity fs camera script 3d unity first person camera what is the best way to make a fps camera in unity unity fpscam unity camera fps objects unity fps camera view camera contoller fps unity camera fps unity c# unity camera fps script 3d unity get camera on first person controller unity third person camera controller make a fps camera unity first person camera controller unity 3d codes unity camera fov unity 3rd person camera controller unity how to make a fps camera camera fps unity 3d unity fps cam how to make a fps movement system in unity 3d first person code how to do a first person camera 3d fps camera unity touch controlo orientation camera unity third person controller unity first person camera scripting first person camera script first person look around code unity c# camera first person how to make a first person game in unity first person canera unity unity fps player camera unity fps camera overlook unity c# fps camera movement script FIRST PERSON MOVEMENT in Unity brackeys code how to make a fps movement in unity easiest way to make first person view in unity unity fps movement tutorial brackeys unity fps unity first person controller tutorial how to make first person controller in unity unity how to make first person controller how to make a first person player unity unity c# 3d 1st person camera movement script unity camera first person first person look unity how to make fps movement mechanics in unity using rigidbody c# code first person fps uinity realistic camera script fps ui\nity realistic camera script first person controller unity c# how to make a player controller in unity 3d player controller in unity 3d unity basic first person controller first person controller unity github fp controller unity how to brackeys player movement script brackes pls help movement script unity 3d third person camera unity how to get fps view unity script unity fps controller script unity third person camera unity character controller unity c# 3D 1st person movement script camrea how to get a first person camera in unity how to get first person camera in unity 2020 first person camera movement unity Unity first person camera and moving is weird first person controller unity android first person camera unity but can look around unity 3d first person camera follow how to make player see in first person 3d unity how to make first person camera in unity 3d unity 3d fps movement character controller script copy paste how to make a first person game unity unity FPS fps movement script unity fps camera movement unity 3d how to get first person in unity how to move in first person unity create first person camera unity unity first person rigidbody controller fps camera code how make good movement unity fps player movement in unity how to make player movement in unity simple first person camera unioty unity fps controller fps camera code unity how to move camera with mouse in unity fps script first person camera how to unity first person camera how to make player flip when pressing a d unity 2d c# create fps controller camera simple fps controller unity how to create a good fps controller in unity unity c# fps contorller fps camera controller unity 3d unity make fps controller firt person script unity 3d unity c# fps camera unity simple fps movement unity first person control unity 3d fps camera script unity 3d fps camera first person cmera unity How to make camera in first person unity first person camera unity script go from main camera to fps in unity unity create first person controller unity camera controller fps simple fps movement unity unity 3d first person movement unity first person camera script hpw to make firt person controls in unity camera first person unity How to make first person player movement script unity camera script unity fps unity rigidbody fps movement tutorial how to add movement fps in unity 3d how to make a fps character controller in unity how to make a game in unity first person unity 3d fps player look how to create a first person camera in unity 1st person camera move to where the camera is looking at unity 1st person camera script unity unity first person look Create first person movementy in Unity move camera unity fps how to make a first person view in unity how to do first person movement in unity first person unity script brackeys 3d character controller first person camera movement script unity how to make first person camera in unity brackeys first person character how to get fps camera in unity how to make first person movement in unity 2020 Camera follow player unity fps script 3d how to make an fps camera in unity how to make a character move in unity 3d brackeys first person character controller unity unity fp camera how to create a first person controller in unity how to make a good movement based fps controller in unity unity camera fps movement parkour fps movemt unity how to create a first person player in unity add first person movement to unityy fps camera control unity how to make a fps style camera in unity 2020 how to make a fps style camera in unity first person controller unity unity 3d fist person camera script unity fps camera controller unity how to make firsto person camera unity camera control fps easy fps controller unity how to make a fps controllor in unity from scratch how to make fps movement in unity how to make an fps controller in unity how to make 1st person camera unity unity how to make first person camera basic 3d player first personcamera unity unity first person camera controller fps camera unity' creating a first person controller in unity fps camera controller unity unity fps camera movement 1st person camera movement unity c# Make 1st person camera in Unity unity ways to make fps controller how to make first person movement in unity how to make an fps character look around unity unity fps camera camera fps unity unity make first person camera 1st person camera unity first person player camera unity3d first person controller script unity unity fps looking around is weird how to make fps camera in unity Unity basic fps controller camera movement 3d unity fps create a first person controller unity unity fps controller tutorial how to make a 1st person camera in unity Unity create first person camera Unity 1st person camera script basic fps controller unity first person c# mobile fps movement unity unity first person character controller first person camera folow scirpt unity 1st person controller unity fps movement unity brackeys unity basic first person camera controller how to make first person controlles unity first person unity 3d controller script fps controls in unity FIX 360 fps camera unity premade firts person controller unity unity first person controller create fps controller unity super simple fps controller unity first person camera script unity how to make a fps controller in unity camera movement fps c# unity fps cAMERA C# unity first person setup how to make a first person controller in unity brackeys first person controller how to make a first person character in unity fps camera unity how to make a first person camera in unity how to do fpc in unity FPS CAMERA WITHOUT MOUSE CONTROLL first person camera unity fps mobile camera script unity 3d first person camera controller unity unity fps camera script first person camera unity unity fps camera movement 3d unity how to write a first person camera unity 3d first person camera unity first person camera unity camera movement fps fps control unity camera unity basic fps camera unity first person walking script copy and paste unity first person movement script copy and paste first person unity ode unity first person controller script fps camera script unity fps camera unity c# unity camera fps simple fps camera unity
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source