Always wanted to do one of these. I have almost no experience, but I’m picking up Unity pretty quick. The rules said to post any code we’ll be using, so here’s my First Person Controller, which is a modified version of Quill18’s amazing video tutorial:
using UnityEngine;
using System.Collections;
public class FirstPersonControllerV2 : MonoBehaviour {
CharacterController player;
float verticalvelocity = 0;
float pitchlook = 0;
float wsspeed = 0;
float adspeed = 0;
float jumpspeed = 0;
float falsegravity = 9.81f;
public float mousesensitivity = 5.5f;
// Use this for initialization
void Start () {
Screen.lockCursor = true;
player = GetComponent();
}
// Update is called once per frame
void Update () {
//MouseX
float yawlook = Input.GetAxis("MouseX") * mousesensitivity;
transform.Rotate(0,yawlook,0);
//MouseY
pitchlook -= Input.GetAxis("MouseY") * mousesensitivity;
pitchlook = Mathf.Clamp(pitchlook, -60, 60);
Camera.main.transform.localRotation = Quaternion.Euler(pitchlook,0,0);
//Gravity
verticalvelocity -= falsegravity * Time.deltaTime;
//Movement
if(player.isGrounded){
falsegravity = 8.81f;
if(Input.GetKey(KeyCode.LeftShift)){
wsspeed = Input.GetAxis("Vertical") * 17.5f;
adspeed = Input.GetAxis("Horizontal") * 17.5f;
}
else{
wsspeed = Input.GetAxis("Vertical") * 9.6f;
adspeed = Input.GetAxis("Horizontal") * 9.6f;
}
//Jumping
if(Input.GetButtonDown("Jump")){
falsegravity = 35f;
verticalvelocity = 15f;
if(Input.GetKey(KeyCode.LeftShift)){
jumpspeed = 17.5f;
}
else{
jumpspeed = 9.6f;
}
}
}
else{
wsspeed = Input.GetAxis("AirVertical") * jumpspeed;
adspeed = Input.GetAxis("AirHorizontal") * jumpspeed;
}
Vector3 speed = new Vector3(adspeed, verticalvelocity, wsspeed);
speed = transform.rotation * speed;
player.Move(speed * Time.deltaTime);
}
}
The modifications were made to make the movement much faster when sprinting while giving it a more “DOOM-y” feel. By altering the gravity and sensitivity of the different input axes in “Edit> Project Settings> Input” you can give the movement the same slide as older FPS games, or disable it altogether. You will need to create two new Axes in that editor called “AirHorizontal” and “AirVertical” and copy the settings of “Horizontal” and “Vertical” exactly. Then by altering the Sensitivity and Gravity settings, Jumping can have a different level of precision as running without changing the actual speed of movement. For me, that means ground movement has instant change and a slight slide when stopping, while jumping requires time to change direction, but speed is maintained.
Hope this helps somebody else!