"unity first person controller script" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "unity first person controller script" answered properly. Developers are finding an appropriate answer about unity first person controller script related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like unity first person controller script. Enter your desired code related query in the search bar and get every piece of information about C# code related question on unity first person controller script. 

first person camera controller unity

By Zany ZebraZany Zebra on May 25, 2020
//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;
		}
	}
}

Add Comment

9

Basic fps camera C#

By Aggressive AlbatrossAggressive Albatross on Dec 23, 2019
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);
	}
}

Add Comment

4

fps movment unity

By GameFlowGameFlow on Sep 28, 2020
public float walkSpeed = 6.0F;
public float jumpSpeed = 8.0F;
public float runSpeed = 8.0F;
public float gravity = 20.0F;

private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;

void Start()
{
    controller = GetComponent<CharacterController>();
}

void Update()
{
    if (controller.isGrounded)
    {
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= walkSpeed;
        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;
    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime);
}

Add Comment

0

how to make a first person controller in c#

By Graceful GoatGraceful Goat on Nov 08, 2020
     public float speed = 10.0f;
     public float straffeSpeed = 7.0f;
     public float jumpHeight = 3.0f;
     Rigidbody rb;
     
     
 
     // Start is called before the first frame update
     void Start()
     {
         Cursor.lockState = CursorLockMode.Locked;
         rb = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
         float DisstanceToTheGround = GetComponent<Collider>().bounds.extents.y;
         bool IsGrounded = Physics.Raycast(transform.position, Vector3.down, DisstanceToTheGround + 0.1f);
 
         float translation = Input.GetAxis("Vertical") * speed;
         float straffe = Input.GetAxis("Horizontal") * straffeSpeed;
         translation *= Time.deltaTime;
         straffe *= Time.deltaTime;
 
 
         //anim.SetTrigger("isWalking");
 
         transform.Translate(straffe, 0, translation);
         //rb.velocity = transform.forward * translation + transform.right * straffe ;
         
 
         if(IsGrounded && Input.GetKeyDown("space"))
         {
             Debug.Log("JUMP!");
             rb.velocity = new Vector3(0, jumpHeight, 0);            
         }
         
         if (Input.GetKey(KeyCode.R))
         {
             Scene scene = SceneManager.GetActiveScene(); 
             SceneManager.LoadScene(scene.name);
         }
 
         if (Input.GetKeyDown("escape"))
         {
             Application.Quit();
             //Cursor.lockState = CursorLockMode.None;
         }
 
     }
 }
 

Add Comment

0

first person camera controller unity

By Zany ZebraZany Zebra on May 21, 2020
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;
		}
	}
}

Add Comment

0

All those coders who are working on the C# based application and are stuck on unity first person controller script can get a collection of related answers to their query. Programmers need to enter their query on unity first person controller script related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about unity first person controller script for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of unity first person controller script while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "unity first person controller script". Visit this developer's friendly online web community, CodeProZone, and get your queries like unity first person controller script resolved professionally and stay updated to the latest C# updates. 

C# answers related to "unity first person controller script"

View All C# queries

C# queries related to "unity first person controller script"

unity first person controller script unity hide mouse first person simple first person camera unioty unity alternative to controller.move unity menu controller code unity controller input get the first letter of a string unity unity float from another script C# player movement script unity unity change tmp text from script unity set particle properties through script how do i limit the amount of prefabs in unity using c# script unity can't put tmpro in script how to make a follow script in unity unity set sprite image from script character control script unity How to make unity script editor open in visual studio not in Note pad how to access the dictionary from another script in unity unity public script gravity script unity how to change text to bold through script unity unity unfreeze position in script unity script wait unity die script unity move script if else how to reference a static variable from another script in unity unity script template folder HOW TO SET TAG IN SCRIPT UNITY unity how to get a script that is in Editor folder bounce script unity enemy turret one direction ahooting script unity 2d unity remove component in script unity movement script 3d get int from another script unity unity teleport script 3D unity character movement script unity3d invector expand fsm controller how to retrive an enum string value instead of number in c# controller how to add force to character controller asp.net mvc hide div from controller how to use hangfire in controller in .net core how to pass view data to controller in mvc mouse click unity raycast unity find first occurrence of character in string c# words return first 20 items of array how to update model in entity framework db first approach first or default c# automapper how to show a first item in a combobox in c# winforms c# skip only first element in array entity save example in c# model first replace first occurrence of character in string c# .net core 3 entity framework constraint code first image field find first monday of every month algorithm c# get first and last item list c# There is already an open DataReader associated with this Command which must be closed first. There is already an open DataReader associated with this Connection which must be closed first. freeze axis in script what function is called just before the a script is ended c# how to assign 2d physics material through script reference variable from another script "winforms" c# can't add an editor script How to execute a script after the c# function executed isGrounded script for copy google script get time premade movement script c# google script get font color how to lock and hide a cursor unity reload scene unity header in inspector unity unity how to convert mouse screen position to world position how to detect a mouse click in unity unity mirror get ip address unity textmesh pro how to just aet z rotation on transform unity unity how to get y value unity mouse position to world unity c# set gameobject active Time delay C# unity unity check collider layer unity log unity print to console unity to string delete in unity fps camera unity unity how to set gameobjkect enabled dropdown text mesh pro unity unity mouse scroll wheel axis how to convert int to string unity c# object spin unity round to float unity require component unity Unity if or how to disable and enable rigidbody unity unity change text color unity set object scale Unity C# instantiate prefab unity transformer double en float How to create a list in csharp unity debug.log unity unity destroy all objects with tag load scene unity unity lerp position Debug unity unity instantiate prefab unity failed to load window layout unity c# transform position unity c# check how many of an object exists unity c# change image source destroy all objects under parent unity c# how to change the color of an object in unity c# rgb unity lerp toggle unity c# unity instantiate prefab as child how to change scenes in unity convert array to list Unity C# how to clamp a velocity in unity c# index out of bound unity c# press key run code unity c# unity c# static monobehaviour unity c# write line unity c# method after delay translate gameobject unity c# Disable Debug.log Unity load material unity c# how to add a variable in unity c# unity c# sin get shader unity c# unity c# find object position in array collision detector unity c# 2d how to make float in unity c# unity c# flip sprite unity read text file play sound on collision unity c# unity cancel invokerepeating unity create empty gameobject in code unity restore deleted files moving camera with touch screen unity random position unity 2d how to store some variables on the device in unity unity get refresh rate quadratic aiming unity unity ar scale for what is percent sign in unity c# how to download something form the web unity unity state machine behaviour for ai hwo to check if something is in a sphere in unity unity animator trigger stuck unity animation missing gameobject replace unity 2d enemy field of view character stay in ground unity 3d unity check if animator has parameter simple enemy spawning Unity unity smooth movement lerp how to save data on cellphone unity android parse float not working unity change z value unity unity stop object from rotating unity set parent canvas how to set minvalue of slider in unity compass direction mobile unity unity var not minus unity c# 10 random numbers unity unity rename populated list varibale true false when key pressed in c sharp unity unity android app black screen unity c# public all codes how to remove a parten transform unity unity check if right keyboard button is down unity smooth rotation 2sd layermask in unity unity get sign of number Unity make a homing object gaussian blur unity sprite 2D how to make % posibility to spawn an object C# in unity On add component unity unity c# jump how to do a sculpt in unity how to add colider in obj in unity 2020 destroy ui element unity unity predicts rigidbody position in x seconds clock in unity Player movement with animation unity object escape player unity change character velocity unity unity check if current scene is being unloaded unity clear array unity insert variable into string player ToJson unity buttons not working canvas group unity unity black screen unity find out sdk version in code Unity how to put IEnumerator in update and loop once with yeild return new waitforseconds refrerencing tags unity unity Couldn't acquire device ID for device name Built-in Microphone unity 2d swap out background image unity text change percentage how to import camera pos in unity

Browse Other Code Languages

CodeProZone