Basic fps camera C#
C#
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);
}
}
Also in C#:
- Title
- c# add object to array
- Category
- C#
- Title
- how to make error sound c#
- Category
- C#
- Title
- isdaylightsavingtime in c#
- Category
- C#
- Title
- c# long to int
- Category
- C#
- Title
- c# creating exceptions
- Category
- C#
- Title
- Unity how to put IEnumerator in update and loop once with yeild return new waitforseconds
- Category
- C#
- Title
- c# thread sleep
- Category
- C#
- Title
- asp.net core razor pages upload file
- Category
- C#
- Title
- asp.net model display name
- Category
- C#
- Title
- c# empty char
- Category
- C#
- Title
- how to compare datetime in c#
- Category
- C#
- Title
- c# append to file
- Category
- C#
- Title
- transform object according to its parent unity
- Category
- C#
- Title
- how to reference a child object unity
- Category
- C#
- Title
- c# check file exists
- Category
- C#
- Title
- kill child C#
- Category
- C#
- Title
- Execute code every x seconds with Update()
- Category
- C#
- Title
- inheritance c#
- Category
- C#
- Title
- c# duplicate object instance
- Category
- C#
- Title
- for each property in object c#
- Category
- C#
- Title
- unity up arrow input
- Category
- C#
- Title
- c# build string out of list of strings
- Category
- C#
- Title
- unity round vector 3 to nearest integer
- Category
- C#
- Title
- using mediamanager how to play mp3 files
- Category
- C#
- Title
- making a list of chars in c#
- Category
- C#
- Title
- how can convert string to int csharp
- Category
- C#
- Title
- Read csv file into wpf C#
- Category
- C#
- Title
- c# check if string is only letters and numbers
- Category
- C#
- Title
- using serial port in c#
- Category
- C#
- Title
- unity scene name get
- Category
- C#
- Title
- c# property get set
- Category
- C#
- Title
- unity round
- Category
- C#
- Title
- c# type of generic is string
- Category
- C#
- Title
- what data type should be for contact number in asp.net
- Category
- C#
- Title
- c# how to add newline on text box
- Category
- C#
- Title
- object escape player unity
- Category
- C#
- Title
- how to hide and show object in unity script
- Category
- C#
- Title
- null coalesce ternary c#
- Category
- C#
- Title
- how to clamp transform.rotation
- Category
- C#
- Title
- unity accessing 2d pointlight from c# script
- Category
- C#
- Title
- how to pass object as test case in nunit c#
- Category
- C#
- Title
- covert char[] to string C#
- Category
- C#
- Title
- unity print to console
- Category
- C#
- Title
- Instantiate c#
- Category
- C#
- Title
- check if two timespans intersect c#
- Category
- C#
- Title
- attribute usage c#
- Category
- C#
- Title
- .net identity seed users and roles
- Category
- C#
- Title
- Request.ServerVariables["HTTP_X_FORWARDED_FOR"] get only one ipaddress
- Category
- C#
- Title
- c# index in select
- Category
- C#
- Title
- what does gameobject.find return
- Category
- C#
- Title
- get all child gameObject of gameObject C#
- Category
- C#
- Title
- move file from one folder to another c#
- Category
- C#
- Title
- unity add component
- Category
- C#
- Title
- split string in c#
- Category
- C#
- Title
- how to insert into a list c#
- Category
- C#
- Title
- how to convert string to bool c#
- Category
- C#
- Title
- .net loop through dictionary
- Category
- C#
- Title
- c# new list object
- Category
- C#
- Title
- c# iorderedenumerable to dictionary
- Category
- C#
- Title
- c# expression func automatically select return type
- Category
- C#
- Title
- how to delay something in c# unity
- Category
- C#
- Title
- c# access session in class
- Category
- C#
- Title
- unity 2d platformer movement script c#
- Category
- C#
- Title
- nullable unique constraint ef
- Category
- C#
- Title
- c# zip a file
- Category
- C#
- Title
- how to crouch in unity
- Category
- C#
- Title
- c# send email
- Category
- C#
- Title
- unity how to end a game with esc
- Category
- C#
- Title
- how to remove file changes in git
- Category
- C#
- Title
- c# stop loop
- Category
- C#
- Title
- C# for
- Category
- C#
- Title
- c# multiple catch exceptions
- Category
- C#
- Title
- split on uppercase c#
- Category
- C#
- Title
- unity docs player input
- Category
- C#
- Title
- get out of foreach statement c#
- Category
- C#
- Title
- call Textboxfor in cs
- Category
- C#
- Title
- c# get country code
- Category
- C#
- Title
- enums c#
- Category
- C#
- Title
- no entity framework provider found for the ado.net provider with invariant name
- Category
- C#
- Title
- parsing object from text file c#
- Category
- C#
- Title
- c# console writeline color
- Category
- C#
- Title
- get user directory of file in c#
- Category
- C#
- Title
- no overload for 'useItemOnSceneLoad' matches delegate 'UnityAction<Scene, LoadSceneMode>'
- Category
- C#
- Title
- mvc write to console
- Category
- C#
- Title
- unity input get axis
- Category
- C#
- Title
- c# httpclient azure function authorization
- Category
- C#
- Title
- c# enum.getvalues
- Category
- C#
- Title
- c# get foreground window
- Category
- C#
- Title
- c# integer to bit string
- Category
- C#
- Title
- generate a dropdown list from array data using razor .net mvc
- Category
- C#
- Title
- c# replace foreach with lambda
- Category
- C#
- Title
- git set origin
- Category
- C#
- Title
- how to switch scenes unity
- Category
- C#
- Title
- c# remove from list in foreach
- Category
- C#
- Title
- c# how to run external program
- Category
- C#
- Title
- unity rb.addexplosionforce 2d
- Category
- C#
- Title
- unity rotate around point
- Category
- C#
- Title
- unity deactive all object in list
- Category
- C#
- Title
- how to trigger event when a com device is connected in c#
- Category
- C#
- Title
- c# list remove item based on property duplicate
- Category
- C#