first person camera controller unity
C#
//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, rotY, 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;
}
}
}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, rotY, 0);
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
if (Cursor.visible && Input.GetMouseButtonDown(1))
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
}
Also in C#:
- Title
- downlaod file and use C#
- Category
- C#
- Title
- unity how to change rotation
- Category
- C#
- Title
- unity get list length
- Category
- C#
- Title
- unity rotate around axis
- Category
- C#
- Title
- how to find the area of a circle
- Category
- C#
- Title
- unity rotate object c#
- Category
- C#
- Title
- viewBag as a list
- Category
- C#
- Title
- c# get current date
- Category
- C#
- Title
- transform.translate unity
- Category
- C#
- Title
- c# get all class properties
- Category
- C#
- Title
- c# filter non alphanumeric characters
- Category
- C#
- Title
- max of array C#
- Category
- C#
- Title
- c# reverse string
- Category
- C#
- Title
- when do i need to end a sentence with ; in c#
- Category
- C#
- Title
- c# implement ienumerable t
- Category
- C#
- Title
- trello
- Category
- C#
- Title
- pop up element from specific index in array
- Category
- C#
- Title
- json serialize object capitalization config
- Category
- C#
- Title
- how to find a transform unity
- Category
- C#
- Title
- Could not load file or assembly 'Ubiety.Dns.Core, Version=2.2.1.0
- Category
- C#
- Title
- if get key down unity
- Category
- C#
- Title
- iterate through dictionary c#
- Category
- C#
- Title
- what function is called just before the a script is ended c#
- Category
- C#
- Title
- call a function c#
- Category
- C#
- Title
- idbset sqlquery
- Category
- C#
- Title
- set label position winforms
- Category
- C#
- Title
- transform object according to its parent unity
- Category
- C#
- Title
- c# loop datatable rows
- Category
- C#
- Title
- letter at index of string c#
- Category
- C#
- Title
- .net directorysearcher get manager accountname
- Category
- C#
- Title
- enums c#
- Category
- C#
- Title
- how to make a pause feautre in unity
- Category
- C#
- Title
- c# mailmessage set sender name
- Category
- C#
- Title
- mailkit send attachment
- Category
- C#
- Title
- .net identity seed users and roles
- Category
- C#
- Title
- onmouseclick unity
- Category
- C#
- Title
- c sharp substring
- Category
- C#
- Title
- C# .net core convert int to enum
- Category
- C#
- Title
- asp.net core 3.1: cast jObject to dictionary<string,string>
- Category
- C#
- Title
- 2D follow ia unity 2D with agrorange
- Category
- C#
- Title
- unity round
- Category
- C#
- Title
- find gameobject with tag
- Category
- C#
- Title
- set int to null c#
- Category
- C#
- Title
- c# int
- Category
- C#
- Title
- how to add an embedded resource in visual studio code
- Category
- C#
- Title
- replace double backslash with single backslash c#
- Category
- C#
- Title
- c# if statement
- Category
- C#
- Title
- background color with opacity
- Category
- C#
- Title
- unity how to change the text on a button
- Category
- C#
- Title
- c sharp comments
- Category
- C#
- Title
- c# keyboard enter
- Category
- C#
- Title
- .net core authorizationhandlercontext
- Category
- C#
- Title
- regex in c#
- Category
- C#
- Title
- eager loading vs lazy loading c#
- Category
- C#
- Title
- unity set list of strings
- Category
- C#
- Title
- c# string list contains
- Category
- C#
- Title
- c# get index of item in list
- Category
- C#
- Title
- how to mock abstract httpcontext using moq .net core
- Category
- C#
- Title
- how to see if they are aholding down a key unity
- Category
- C#
- Title
- wpf use enum description
- Category
- C#
- Title
- how to do cmd command c#
- Category
- C#
- Title
- asp net identity include phone number when registering
- Category
- C#
- Title
- instantiate object in circle
- Category
- C#
- Title
- unity variable from another script
- Category
- C#
- Title
- how to put double quotes in a string c#
- Category
- C#
- Title
- c sharp add item to dictionary
- Category
- C#
- Title
- how to draw a dot in c#
- Category
- C#
- Title
- how to hide a panel ui unity
- Category
- C#
- Title
- How to get number of months between 2 dates c#
- Category
- C#
- Title
- c# start process
- Category
- C#
- Title
- unity button interactable
- Category
- C#
- Title
- c# uppercase string
- Category
- C#
- Title
- what is using static in c#
- Category
- C#
- Title
- c# print
- Category
- C#
- Title
- rotation unity script 2d
- Category
- C#
- Title
- c# stop loop
- Category
- C#
- Title
- c# one line set
- Category
- C#
- Title
- mongodb c# batch find
- Category
- C#
- Title
- c# md5 string
- Category
- C#
- Title
- array c#
- Category
- C#
- Title
- console.writeline
- Category
- C#
- Title
- c# unhandled exception in thread
- Category
- C#
- Title
- c# dynamic object get value
- Category
- C#
- Title
- sum of digits in c#
- Category
- C#
- Title
- unity create gameobject
- Category
- C#
- Title
- c# linq select specific columns
- Category
- C#
- Title
- unity find object by name
- Category
- C#
- Title
- install .net sdk ubuntu 20
- Category
- C#
- Title
- how to load the active scene unity
- Category
- C#
- Title
- c# mongodb update multiple fields
- Category
- C#
- Title
- mouseposition unity
- Category
- C#
- Title
- c# public static string
- Category
- C#
- Title
- exception handling c#
- Category
- C#
- Title
- how to make a game
- Category
- C#
- Title
- check if enabled unity
- Category
- C#
- Title
- check version of asp.net core
- Category
- C#
- Title
- combine two arraylist c#
- Category
- C#
- Title
- how to store some variables on the device in unity
- Category
- C#
- Title
- c# replace string case insensitive
- Category
- C#
- Title
- c# find element by condition
- Category
- C#