fps controller c#
C#
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
[SerializeField] float speed = 3f;
[SerializeField] float sensitivity = 150f;
[SerializeField] float jumpHeight = 4f;
[Space]
[SerializeField] Camera cam = null;
[SerializeField] Rigidbody rb;
float clampedXRot;
bool onGround = true;
void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Movement()
{
float _xMove = Input.GetAxis("Horizontal"); // Defines Input Axes
float _zMove = Input.GetAxis("Vertical");
float _xRot = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
float _yRot = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
Vector3 movVert = _zMove * transform.forward;
Vector3 movHor = _xMove * transform.right;
Vector3 velocity = (movHor + movVert) * speed * Time.deltaTime;
Vector3 rotation = new Vector3(0f, _yRot, 0f);
clampedXRot += _xRot;
clampedXRot = Mathf.Clamp(clampedXRot, -90f, 90f);
transform.Rotate(rotation);
rb.MovePosition(rb.position + velocity);
cam.transform.localRotation = Quaternion.Euler(clampedXRot, 0f, 0f); // Parent the camera to the player and reset the transform
}
void Jump()
{
if (Input.GetButton("Jump"))
{
if (onGround == true)
{
rb.velocity = new Vector3(0f, jumpHeight, 0f);
onGround = false;
}
}
}
void FixedUpdate()
{
if (transform != null)
{
Movement();
Jump();
}
}
private void OnCollisionEnter(Collision collision) // Give the ground the tag "Ground"
{
if (collision.collider.tag == "Ground")
{
onGround = true;
}
}
}
Also in C#:
- Title
- c# tostring mmm dd yyyy
- Category
- C#
- Title
- c# string list contains
- Category
- C#
- Title
- c# reverse string
- Category
- C#
- Title
- how to clear console through script unity
- Category
- C#
- Title
- insert variables into string c#
- Category
- C#
- Title
- how to get specific length of row in matrix c#
- Category
- C#
- Title
- console.writeline
- Category
- C#
- Title
- page parent wpf
- Category
- C#
- Title
- c# get all class properties
- Category
- C#
- Title
- c# get country code
- Category
- C#
- Title
- c# index in select
- Category
- C#
- Title
- unity round vector 3 to nearest integer
- Category
- C#
- Title
- parsing object from text file c#
- Category
- C#
- Title
- wpf binding ancestor codebehind
- Category
- C#
- Title
- how to make a game
- Category
- C#
- Title
- get type of variable c#
- Category
- C#
- Title
- json tiers dot in name c#
- Category
- C#
- Title
- c# typeof
- Category
- C#
- Title
- c# try catch error
- Category
- C#
- Title
- c# get index of item in list
- Category
- C#
- Title
- unity assign button onclick
- Category
- C#
- Title
- c# join array
- Category
- C#
- Title
- flip boolean c#
- Category
- C#
- Title
- linq c# object except two lists
- Category
- C#
- Title
- as c#
- Category
- C#
- Title
- unity c# change color of gameobject
- Category
- C#
- Title
- windows forms iterate through all controls
- Category
- C#
- Title
- south african id number validation c#
- Category
- C#
- Title
- c# groupby date
- Category
- C#
- Title
- text variable type unity
- Category
- C#
- Title
- c# property get set
- Category
- C#
- Title
- winforms c# add data to datagridview with a button
- Category
- C#
- Title
- .net loop through dictionary
- Category
- C#
- Title
- C# int to hex
- Category
- C#
- Title
- c# empty char
- Category
- C#
- Title
- how to add to an array c#
- Category
- C#
- Title
- unity particle system color
- Category
- C#
- Title
- c# stop loop
- Category
- C#
- Title
- replace double backslash with single backslash c#
- Category
- C#
- Title
- .net identity seed users and roles
- Category
- C#
- Title
- unity c# write line
- Category
- C#
- Title
- C# events
- Category
- C#
- Title
- c# merge two lists as queryable
- Category
- C#
- Title
- get specific object in list c#
- Category
- C#
- Title
- c# exit for
- Category
- C#
- Title
- c# linq order by multiple columns
- Category
- C#
- Title
- c# keyboard enter
- Category
- C#
- Title
- c# to binary
- Category
- C#
- Title
- c sharp string interpolation
- Category
- C#
- Title
- lista generica como parametro de un metodo en c#
- Category
- C#
- Title
- c# changimg to one decimal place
- Category
- C#
- Title
- how to start grid from where the data starts in c# charts
- Category
- C#
- Title
- c# error CS0120
- Category
- C#
- Title
- unity set position
- Category
- C#
- Title
- C# enum
- Category
- C#
- Title
- what is using static in c#
- Category
- C#
- Title
- how to destroy a gameobject after some hits in unity 3d
- Category
- C#
- Title
- c# string formatting
- Category
- C#
- Title
- unity to integer
- Category
- C#
- Title
- display none asp.net
- Category
- C#
- Title
- abstract class c#
- Category
- C#
- Title
- c# replace foreach with lambda
- Category
- C#
- Title
- c# get gridview DataKeyNames
- Category
- C#
- Title
- linq c# or
- Category
- C#
- Title
- dynamic in c#
- Category
- C#
- Title
- how to go to other forms in C#
- Category
- C#
- Title
- c# merge two xml files
- Category
- C#
- Title
- iterate through xpdictionary devexpress
- Category
- C#
- Title
- Could not load file or assembly 'Ubiety.Dns.Core, Version=2.2.1.0
- Category
- C#
- Title
- c# sort array
- Category
- C#
- Title
- modal dismiss
- Category
- C#
- Title
- asp.net c# write string to text file
- Category
- C#
- Title
- moq raise event
- Category
- C#
- Title
- c# main method
- Category
- C#
- Title
- Movement 2d unity
- Category
- C#
- Title
- set rotation to velocity unity
- Category
- C#
- Title
- .net core download image from url binary file
- Category
- C#
- Title
- image filter
- Category
- C#
- Title
- unity rotate object relative to camera
- Category
- C#
- Title
- multidimensional meaning
- Category
- C#
- Title
- How to make a function in C#
- Category
- C#
- Title
- how to add object in dictionary in c#
- Category
- C#
- Title
- run async method parallel c#
- Category
- C#
- Title
- unity inspector header attribute
- Category
- C#
- Title
- c# replace string case insensitive
- Category
- C#
- Title
- c# return multiple value unity
- Category
- C#
- Title
- comment envoyer un socket C#
- Category
- C#
- Title
- unity get all by tag
- Category
- C#
- Title
- how to mock abstract httpcontext using moq .net core
- Category
- C#
- Title
- downlaod file and use C#
- Category
- C#
- Title
- C# public 2d array
- Category
- C#
- Title
- C# .net core convert string to enum
- Category
- C#
- Title
- c# string to datetime
- Category
- C#
- Title
- get random point within radius
- Category
- C#
- Title
- question mark operator in c#
- Category
- C#
- Title
- transform object according to its parent unity
- Category
- C#
- Title
- c# .net core memory cache
- Category
- C#
- Title
- c# cheat sheet
- Category
- C#
- Title
- c# empty array
- Category
- C#
- Title
- c# to vb.net
- Category
- C#