I am making a 3D first-person shooter for my class and I ran into a major problem. After following my classes’ playercontroller script tutorial, my player wouldn’t move. For a split second, my player did move (I didn’t change anything to the script) but my gravity was extremely messed up and my character was basically flying when I would jump. I’ll add the player controller script I used for my player and include screenshots of my player’s inspector. If anyone knows where the error is, or knows how to fix this, please let me know I am desperate. Thank you ![]()
public class PlayerController : MonoBehaviour
{
[Header(“Settings”)]
[Tooltip(“The speed at which the player moves”)]
public float moveSpeed = 2f;
[Tooltip(“The speed at which the player rotates to look left and right (calculated in degrees)”)]
public float lookSpeed = 60f;
[Tooltip(“The power in which the player jumps”)]
public float jumpPower = 8f;
[Tooltip(“The strength of gravity”)]
public float gravity = 9.81f;
[Header(“Jump Timing”)]
public float jumpTimeLeniency = 0.1f;
float timeToStopBeingLenient = 0;
[Header("Required References")]
[Tooltip("The player shoot script that fires projectiles")]
public Shooter playerShooter;
public Health playerHealth;
public List<GameObject> disableWhileDead;
bool doubleJumpAvailable = false;
// The character controller component on the player
private CharacterController controller;
private InputManager inputManager;
/// <summary>
/// Description:
/// Standard Unity function called once before the first Update call
/// Input:
/// none
/// Return:
/// void (no return)
/// </summary>
void Start()
{
setUpCharacterController();
setUpInputManager();
}
private void setUpCharacterController()
{
controller = GetComponent<CharacterController>();
if (controller == null)
{
Debug.LogError("The player controller script does not have a character controller on the same game object!");
}
}
void setUpInputManager()
{
inputManager = InputManager.instance;
}
/// <summary>
/// Description:
/// Standard Unity function called once every frame
/// Input:
/// none
/// Return:
/// void (no return)
/// </summary>
void Update()
{
if(playerHealth.currentHealth <= 0)
{
foreach(GameObject inGameObject in disableWhileDead)
{
inGameObject.SetActive(false);
}
return;
}
else
{
foreach (GameObject inGameObject in disableWhileDead)
{
inGameObject.SetActive(true);
}
}
processMovement();
proccessRotation();
}
Vector3 moveDirection;
void processMovement()
{
//Get the input from the input manager
float leftRightInput = inputManager.horizontalMoveAxis;
float forwardBackwardInput = inputManager.verticalMoveAxis;
bool jumpPressed = inputManager.jumpPressed;
//Handle the control of the player while it is on the ground
if (controller.isGrounded)
{
doubleJumpAvailable = true;
timeToStopBeingLenient = Time.time + jumpTimeLeniency;
//Set the movement direction to be received input, set y to 0 since we are on the ground
moveDirection = new Vector3(leftRightInput, 0, forwardBackwardInput);
//Set the move direction in relation to the transform
moveDirection = transform.TransformDirection(moveDirection);
moveDirection = moveDirection * moveSpeed;
if (jumpPressed)
moveDirection.y = jumpPower;
}
else
{
moveDirection = new Vector3(leftRightInput * moveSpeed, moveDirection.y, forwardBackwardInput * moveSpeed);
moveDirection = transform.TransformDirection(moveDirection);
if (jumpPressed && Time.time < timeToStopBeingLenient)
{
moveDirection.y = jumpPower;
}
else if (jumpPressed && doubleJumpAvailable)
{
moveDirection.y = jumpPower;
doubleJumpAvailable = false;
}
}
moveDirection.y -= gravity * Time.deltaTime;
if (controller.isGrounded && moveDirection.y <0)
{
moveDirection.y = -0.3f;
}
controller.Move(moveDirection * Time.deltaTime);
}
void proccessRotation()
{
float horizontalLookInput = inputManager.horizontalLookAxis;
Vector3 playerRotation = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(new Vector3(playerRotation.x, playerRotation.y + horizontalLookInput * lookSpeed * Time.deltaTime, playerRotation.z));
}
}
