Customized Character Controller script issues

I finished a relatively simple character controller in a few minutes. I’ve got one issue with the script. Sometimes, when the axes are flicked quickly, the engine doesn’t detect that movement has stopped, and the character’s viewport will not stop moving, or the player will not stop moving.

I also had to build in my own deadzone through code. I thought the unity input preferences had a setting for deadzone, but it doesn’t seem to work at all.

My third problem, is that when the player isn’t grounded, he still seems to be able to control his movement… I thought I had disabled all movement vector changes, thus disabling the player’s ability to control their fall, but this doesn’t seem to be the case… Why is the player still able to change the direction of the fall when the engine does indeed confirm that the player isn’t grounded, and as such, the movement direction doesn’t get updated…

Is there anything I’m doing wrong here?

using UnityEngine;
using System.Collections;

[AddComponentMenu("Character-Control/First Person Controller")]
[RequireComponent(typeof(CharacterController))]

public class FirstPersonController : MonoBehaviour {

	public float HorizontalSensitivity = 4F;
	public float VerticalSensitivity = 4F;

	public bool invertY = true;

	public float minimumX = -360F;
	public float maximumX = 360F;

	public float minimumY = -60F;
	public float maximumY = 60F;

	public float lookDeadzone = 0.25F;
	public float moveDeadzone = 0.25F;
	
	public float speed = 16F;
	public float gravity = 1F;
	
	float rotationX = 0F;
	float rotationY = 0F;
	
	Vector3 moveDir = Vector3.zero;
	bool grounded = true;
	
	Quaternion originalRotation;

	void Update ()
	{
		if(Input.GetAxis("Horizontal")<lookDeadzone*-1||Input.GetAxis("Horizontal")>lookDeadzone)
		{
			rotationX += Input.GetAxis("Horizontal")*HorizontalSensitivity;
		}
		if(Input.GetAxis("Vertical")<lookDeadzone*-1||Input.GetAxis("Vertical")>lookDeadzone)
		{
			if(invertY)
			{
				rotationY += Input.GetAxis("Vertical")*VerticalSensitivity;
			}
			else
			{
				rotationY -= Input.GetAxis("Vertical")*VerticalSensitivity;
			}
		}

		rotationX = ClampAngle (rotationX, minimumX, maximumX);
		rotationY = ClampAngle (rotationY, minimumY, maximumY);

		Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
		Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);

		transform.localRotation = originalRotation * xQuaternion * yQuaternion;
		if(grounded)
		{
			if(Input.GetAxis("Sideways")<moveDeadzone*-1||Input.GetAxis("Sideways")>moveDeadzone)
			{
				if(Input.GetAxis("Forward")<moveDeadzone*-1||Input.GetAxis("Forward")>moveDeadzone)
				{
					moveDir = transform.TransformDirection(new Vector3(Input.GetAxis("Sideways"),0,Input.GetAxis("Forward")))*speed;
				}
				else
				{
					moveDir = transform.TransformDirection(new Vector3(Input.GetAxis("Sideways"),0,0))*speed;
				}
			}
			else
			{
				if(Input.GetAxis("Forward")<moveDeadzone*-1||Input.GetAxis("Forward")>moveDeadzone)
				{
					moveDir = transform.TransformDirection(new Vector3(0,0,Input.GetAxis("Forward")))*speed;
				}
				else
				{
					moveDir = Vector3.zero;
				}
			}
		}
		moveDir.y -= gravity;

		CharacterController controller = GetComponent<CharacterController>();
		CollisionFlags flags = controller.Move(moveDir*Time.deltaTime);
		grounded = (flags  CollisionFlags.CollidedBelow)!=0;
	}
	
	void Start ()
	{
		if (rigidbody)
			rigidbody.freezeRotation = true;
		originalRotation = transform.localRotation;
	}
	
	public static float ClampAngle (float angle, float min, float max)
	{
		if (angle < -360F)
			angle += 360F;
		if (angle > 360F)
			angle -= 360F;
		return Mathf.Clamp (angle, min, max);
	}
}

I’m not exactly sure how the control system works in your game, but it is possible that the extra travel is caused by Input.GetAxis. This function returns a smoothed value that gradually builds and decays over a short time. The dead zone only applies to analogue input devices. (You can change settings for the time sensitivity and dead zone in the input settings - menu: Edit > Player Settings > Input).

As for the other problem, try putting a Debug.Log call in the “If (grounded)” clause. This will detect whether or not this branch of the code is being taken erroneously or if the direction change is happening elsewhere.