Hello, folks. I’m a brand new developer and coding is definitely the thing I’m weakest at, but I’m determined to teach myself up to at least basic competence. Right now I’m trying to build a simple first-person character controller for use in future projects. It currently exists as the following scripts, which are pretty much unmodified from Holistic3d’s tutorial:
CheckButtons looks for key presses and turns them into translation, while locking that translation to delta time
using UnityEngine;
using System.Collections;
public class CheckButtons : MonoBehaviour {
public float walkingspeed = 1.5f;
void Start () {
Cursor.lockState = CursorLockMode.Locked;
}
void Update () {
float translation = Input.GetAxis ("Vertical") * walkingspeed;
float strafe = Input.GetAxis ("Horizontal") * walkingspeed;
translation *= Time.deltaTime;
strafe *= Time.deltaTime;
transform.Translate (strafe, 0, translation);
if (Input.GetKeyDown ("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
Mouselook interprets mouse movement and rotates the camera (for up/down movement) or character (for left/right movement) as necessary, while smoothing it.
using UnityEngine;
using System.Collections;
public class Mouselook : MonoBehaviour {
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 2.0f;
public float smoothing = 2.0f;
GameObject character;
void Start ()
{
// This causes the "character" GameObject to be the parent of the camera the script is applied to
character = this.transform.parent.gameObject;
}
void Update ()
{
var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);
}
}
I’m at the point where I sorta understand most of what is happening here. Slap the first one on a capsule with X and Z rotation of the rigidbody locked and the second one on a child camera of that capsule and you can wander around the world wherever you want. Next I need to figure out how to add the following:
-Lock the camera so that you can’t flip it upside-down, you can only look straight up or straight down at most.
-Dip the camera slightly when walking (a less obnoxious alternative to headbob)
-Simple sprint, increasing speed to around 5 while holding shift.
This controller will likely never need to duck or jump, and likely won’t ever need to be effected by physics beyond gravity.
I’m still plugging away at this, but I’m still so new to coding that I can barely figure out how to read the documentation, so any advice is appreciated. One specific question I have: What exactly is the “character controller” component added to the default asset FPS controller in addition to the script? I don’t see how it relates to what’s in the script, and my own script seems to work fine without it.