My player won't move, please help?
Hi all,
I've gone through my script but I can't figure out why my player won't move at all? I hit the wasd keys and literally nothing happens.
using System.Collections; using System.Collections.Generic; using UnityEngine;
/// /// This class handles the movement of the player with given input from the input manager /// 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 with which the player junmps")] public float jumpPower = 8f; [Tooltip("The strength of gravity")] public float gravity = 9.81f; [Header("Required References")] [Tooltip("The player shooter script that fires 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();
ProcessRotation();
}
Vector3 moveDirection;
void ProcessMovement()
{
// Get the input from the input manager
float leftRightInput = inputManager.horizontalMoveAxis;
Debug.Log("The left right input value is:" + leftRightInput);
float forwardBackwardInput = inputManager.verticalMoveAxis;
bool jumpPressed = inputManager.jumpPressed;
// Handle the controll of the player while they are on the ground
if (controller.isGrounded)
{
// set movement direction to be recieved input set y 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 *= 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);
}
void ProcessRotation()
{
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));
}
}
Your answer
Follow this Question
Related Questions
GO with rigid body, hierarchy best practices 0 Answers
My Player Is moving all over the place! 0 Answers
c# script for player movement and jumping using touch pad for android 0 Answers
Simultaneously moving using two inputs W D or Up LEFT to move diagonally 0 Answers
playableGraph stop working 1 Answer