Okey, so I learn better from rewriting a piece of code instead of just reading over it. I have taken the FPSWalkerEnhanced script from the Wiki and went through piece by piece rewriting a direct copy. Though much to my dismay once I finished writing it and attached it to my GameObject, the character object would refuse to move given any input.
After going through and looking at each script side by side, I haven’t been able to find any core differences that would prevent the script from working. In testing I copy and pasted the FPSWalkerEnhanced script into a new file and then attached that to a separate GameObject to find that it functioned without error, leaving me baffled.
If any of you in the community have a spare moment to read over the code used below and possibly point out where I have gone wrong, I would be very grateful, thank you in advance!
using UnityEngine;
using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
// ---------------------------------------------------------------------
#region Class members
public int _hopFactor = 1;
public float _walkSpeed = 5.0f;
public float _runSpeed = 7.0f;
public float _jumpSpeed = 3.0f;
public float _slideSpeed = 12.0f;
public float _gravity = 20.0f;
public float _fallingThreshold = 10.0f;
public float _bumpFactor = 0.75f;
public bool _airControl = false;
public bool _slideWhenOverLimit = false;
public bool _slideOnTaggedObjects = true;
public bool _limitStrafe = true;
public bool _runToggle = false;
private int _jumpTimer;
private float _speed;
private float _fallStart;
private float _slideLimit;
private float _rayDistance;
private bool _grounded = false;
private bool _falling;
private bool _playerControl = false;
private Vector3 _contactPoint;
private RaycastHit _rayHit;
private Vector3 _moveDirection = Vector3.zero;
private CharacterController _controller;
private Transform _transform;
#endregion
// ---------------------------------------------------------------------
#region Start method
public void Start()
{
// Set member values
this._controller = GetComponent<CharacterController>();
this._transform = transform;
this._speed = this._walkSpeed;
this._rayDistance = this._controller.height * 0.5f + this._controller.radius;
this._slideLimit = this._controller.slopeLimit - 0.1f;
this._jumpTimer = this._hopFactor;
}
#endregion
// ---------------------------------------------------------------------
#region FixedUpdate method
public void FixedUpdate()
{
// Create local variables
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
float inputFactor = (inputX != 0.0f inputY != 0.0f this._limitStrafe == true)? 0.7071f : 1.0f;
// Check if grounded
if(this._grounded == true)
{
// Create sliding variable
bool sliding = false;
// Check if player should be sliding
if(Physics.Raycast(this._transform.position, -Vector3.up, out this._rayHit, this._rayDistance) == true)
{
// Is vector greater then slide limit
if(Vector3.Angle(this._rayHit.normal, Vector3.up) > this._slideLimit)
{
// Set sliding value
sliding = true;
}
}
else
{
// Calculate raycast from ray hit point
Physics.Raycast(this._contactPoint + Vector3.up, -Vector3.up, out this._rayHit);
// Is vector greater then slide limit
if(Vector3.Angle(this._rayHit.normal, Vector3.up) > this._slideLimit)
{
// Set sliding value
sliding = true;
}
}
// Check if we are currently falling
if(this._falling == true)
{
// Set falling variable
this._falling = false;
// Check for falling threshold
if(this._transform.position.y < this._fallStart - this._fallingThreshold)
{
Debug.Log("TODO: Process falling damage routine");
}
}
// Is character running
if(this._runToggle == false)
{
// Set the current speed of the character
this._speed = Input.GetButton("Run")? this._runSpeed : this._walkSpeed;
}
// Process character sliding
if((sliding == true this._slideWhenOverLimit == true)
|| (this._slideOnTaggedObjects == true this._rayHit.collider.tag == "Slide"))
{
// Create hit normal local variable
Vector3 hitNormal = this._rayHit.normal;
// Calculate and set new move direction
this._moveDirection = new Vector3(hitNormal.x, -hitNormal.y, hitNormal.z);
Vector3.OrthoNormalize(ref hitNormal, ref this._moveDirection);
this._moveDirection *= this._slideSpeed;
// Remove character control while sliding
this._playerControl = false;
}
else
{
// Calcuelate and set new move direction
this._moveDirection = new Vector3(inputX * inputFactor, -this._bumpFactor, inputY * inputFactor);
this._moveDirection = this._transform.TransformDirection(this._moveDirection) * this._speed;
// Regain character control
this._playerControl = true;
}
// Check if jump button is pressed
if(Input.GetButton("Jump") == false)
{
// Increase jump timer
this._jumpTimer++;
}
else if(this._jumpTimer >= this._hopFactor)
{
// Set jump direction
this._moveDirection.y = this._jumpSpeed;
this._jumpTimer = 0;
}
}
else
{
// Check if we are falling
if(this._falling == false)
{
// Set falling variable
this._falling = true;
// Set falling starting point
this._fallStart = this._transform.position.y;
}
// Check if air control is enabled
if(this._airControl == true this._playerControl == true)
{
// Calculate and set new direction
this._moveDirection.x = inputX * this._speed * inputFactor;
this._moveDirection.z = inputY * this._speed * inputFactor;
this._moveDirection = this._transform.TransformDirection(this._moveDirection);
}
// Apply gravity
this._moveDirection.y -= this._gravity * Time.deltaTime;
/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Debugging, only ever prints to console once!
print("this._moveDirection.x = " + this._moveDirection.x.ToString());
print("this._moveDirection.y = " + this._moveDirection.y.ToString());
print("this._moveDirection.z = " + this._moveDirection.z.ToString());
// Move the character and set grounded variable
this._grounded = (this._controller.Move(this._moveDirection * Time.deltaTime) CollisionFlags.Below) != 0;
}
}
#endregion
// ---------------------------------------------------------------------
#region Update method
public void Update()
{
// Check if run is toggled
if(this._runToggle == true this._grounded == true Input.GetButtonDown("Run"))
{
// Calculate new character speed
this._speed = (this._speed == this._walkSpeed? this._runSpeed : this._walkSpeed);
}
}
#endregion
// ---------------------------------------------------------------------
#region OnControllerColliderHit method
public void OnControllerColliderHit(ControllerColliderHit hit)
{
// Retrieve hit point
this._contactPoint = hit.point;
}
#endregion
}