unity simple fps controller
C#
//Simple 3D FPS controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class SC_FPSController : MonoBehaviour
{
public float walkingSpeed = 7.5f;
public float runningSpeed = 11.5f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public Camera playerCamera;
public float lookSpeed = 2.0f;
public float lookXLimit = 45.0f;
CharacterController characterController;
Vector3 moveDirection = Vector3.zero;
float rotationX = 0;
[HideInInspector]
public bool canMove = true;
void Start()
{
characterController = GetComponent<CharacterController>();
// Lock cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
// We are grounded, so recalculate move direction based on axes
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
// Press Left Shift to run
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpSpeed;
}
else
{
moveDirection.y = movementDirectionY;
}
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
// Move the controller
characterController.Move(moveDirection * Time.deltaTime);
// Player and Camera rotation
if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}
Also in C#:
- Title
- eventos c#
- Category
- C#
- Title
- move towards target unity
- Category
- C#
- Title
- run async methods within a sync process
- Category
- C#
- Title
- unity gizmo draw line
- Category
- C#
- Title
- nullreferenceexception c#
- Category
- C#
- Title
- Update data in db .net
- Category
- C#
- Title
- disable mouse unity
- Category
- C#
- Title
- c# object clone
- Category
- C#
- Title
- external font family uwp c#
- Category
- C#
- Title
- unity detect any key
- Category
- C#
- Title
- json ignore property c#
- Category
- C#
- Title
- get setter c# model
- Category
- C#
- Title
- choose random gameobject from a gameobject list
- Category
- C#
- Title
- unity making homing missile
- Category
- C#
- Title
- linq in c#
- Category
- C#
- Title
- sqlite connection c#
- Category
- C#
- Title
- unity load scene
- Category
- C#
- Title
- what is c# used for
- Category
- C#
- Title
- How to look at an object unity
- Category
- C#
- Title
- c# length of array
- Category
- C#
- Title
- how to add array to list in c#
- Category
- C#
- Title
- c# list to string join
- Category
- C#
- Title
- public enum c#
- Category
- C#
- Title
- c# for loop without increment
- Category
- C#
- Title
- modificare una strinfa in c#
- Category
- C#
- Title
- how to access the dictionary from another script in unity
- Category
- C#
- Title
- create dropdown in datatable c# dynamically
- Category
- C#
- Title
- c# iorderedenumerable to dictionary
- Category
- C#
- Title
- unity resources load
- Category
- C#
- Title
- unity up arrow input
- Category
- C#
- Title
- isdaylightsavingtime in c#
- Category
- C#
- Title
- Move player on planets in unity 2d
- Category
- C#
- Title
- iterate through dictionary c#
- Category
- C#
- Title
- how to make a game
- Category
- C#
- Title
- how to add movement in unity
- Category
- C#
- Title
- c# relaxed boolean cast
- Category
- C#
- Title
- animations for pause menu
- Category
- C#
- Title
- C# loop through array of objet
- Category
- C#
- Title
- kill child C#
- Category
- C#
- Title
- list contains type c#
- Category
- C#
- Title
- unity particle system color
- Category
- C#
- Title
- unity ai wander script
- Category
- C#
- Title
- Celsius to Fahrenheit c#
- Category
- C#
- Title
- c# map
- Category
- C#
- Title
- c# singleton
- Category
- C#
- Title
- transform.Translate movement
- Category
- C#
- Title
- How do i destroy a prefab without the error?
- Category
- C#
- Title
- go right unity
- Category
- C#
- Title
- asp textarea
- Category
- C#
- Title
- mvc c# w3schools
- Category
- C#
- Title
- POST http://localhost:5001/api/v1/identity/login 500 (Internal Server Error) LoginForm.jsx:39 Error: Request failed with status code 500 at createError (createError.js:16) at settle (settle.js:17) at XMLHttpRequest.handleLoad (xhr.js:61)
- Category
- C#
- Title
- convert string array to int C#
- Category
- C#
- Title
- c# tab character
- Category
- C#
- Title
- entity framework core
- Category
- C#
- Title
- unity cast int to float
- Category
- C#
- Title
- usermanager find based on role
- Category
- C#
- Title
- c# null conditional
- Category
- C#
- Title
- json tiers dot in name c#
- Category
- C#
- Title
- visual c#
- Category
- C#
- Title
- c# how to use inovke
- Category
- C#
- Title
- find mongodb c# with task T
- Category
- C#
- Title
- how to check if textbox is empty in c#
- Category
- C#
- Title
- unity rotate around point
- Category
- C#
- Title
- how to trim path in C#
- Category
- C#
- Title
- unity persistent data
- Category
- C#
- Title
- how to set progress openedge driver name for odbc connection c#
- Category
- C#
- Title
- c# arrays of arrays
- Category
- C#
- Title
- c# main method
- Category
- C#
- Title
- .net core copy file in folder to root
- Category
- C#
- Title
- unity c# run a command then wait
- Category
- C#
- Title
- c# how to refresh your binding source
- Category
- C#
- Title
- how to compare datetime in c#
- Category
- C#
- Title
- attribute usage c#
- Category
- C#
- Title
- how to raycast unit
- Category
- C#
- Title
- asp.net textarea disable resize
- Category
- C#
- Title
- c# get foreground window
- Category
- C#
- Title
- c# multiline comment
- Category
- C#
- Title
- c# string array to string
- Category
- C#
- Title
- C# get all child classes of a class
- Category
- C#
- Title
- how to change an int value c#
- Category
- C#
- Title
- new command - latex
- Category
- C#
- Title
- unity how to check object position
- Category
- C#
- Title
- add mime type for woff in web.config
- Category
- C#
- Title
- how to switch scenes unity
- Category
- C#
- Title
- c# zip a file
- Category
- C#
- Title
- c# quick "is" "as"
- Category
- C#
- Title
- c# try catch error
- Category
- C#
- Title
- unity3d raycast
- Category
- C#
- Title
- unity on trigger enter
- Category
- C#
- Title
- get set c#
- Category
- C#
- Title
- C# aspnet how to run a migration
- Category
- C#
- Title
- convert array to list Unity C#
- Category
- C#
- Title
- c# switct case
- Category
- C#
- Title
- c# unity rotate first person controller script
- Category
- C#
- Title
- web api startup add imemory cache object
- Category
- C#
- Title
- c# scene manager
- Category
- C#
- Title
- what is using static in c#
- Category
- C#
- Title
- c# join array
- Category
- C#
- Title
- check if string is email c#
- Category
- C#
- Title
- visual studio clear text script
- Category
- C#