I am writing a script, “PlayerController” from a course. My script (and components) are the exact same as in the video (as verified 12 times).
When I press the A and D keys, the player doesn’t move. However, I have noticed (in the inspector) that the horizontal move axis does change when pressing those keys.
Here’s the code I have:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// This class handles the movement of the player with given input from the input manager
/// </summary>
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 keft and right (calculated in degrees")]
public float lookSpeed = 60f;
[Tooltip("The power with which the player jumps")]
public float jumpPower = 8f;
[Tooltip("The strength of gravity")]
public float gravity = 9.81f;
[Header("Required References")]
[Tooltip("The player shooter script that fire projectiles")]
public Shooter playerShooter;
// 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()
{
ProcessMovement();
}
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)
{
// Set the movement direction to be the received input, set y to 0 since we are on the ground
Vector3 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);
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Can you please help me to identify any mistakes? I appreciate it. Thanks.