Character Controller which suddenly has stopped working (Move() does not seem to do anything)
See code attached for reference or if there's anything that has stopped it to behave
public class CharacterMovement3D : MonoBehaviour
{
public float movementSpeed = 6f;
public float gravity = -9.81f;
public float jumpForce = 2f;
public float jumpSpeed = -3;
Controls playerInput;
CharacterController _charController;
Animator characterAnimator;
Vector2 moveInput;
Vector3 direction;
Vector2 playerVelocity;
bool jumpPressed;
bool groundedPlayer;
protected Joystick joystick;
protected Joybutton joybutton;
private void Awake()
{
_charController = GetComponent<CharacterController>();
characterAnimator = GetComponent<Animator>();
playerInput = new Controls();
playerInput.PlayerMovement.Move.performed += ctx => moveInput = ctx.ReadValue<Vector2>();
playerInput.PlayerMovement.Jump.started += ctx => jumpPressed = true;
playerInput.PlayerMovement.Jump.canceled += ctx => jumpPressed = false;
}
// Start is called before the first frame update
void Start()
{
joystick = FindObjectOfType<Joystick>();
joybutton = FindObjectOfType<Joybutton>();
}
// Update is called once per frame
void Update()
{
groundedPlayer = _charController.isGrounded;
moveInput.x = moveInput.x + joystick.Horizontal;
direction = new Vector3(moveInput.x, 0, 0).normalized;
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = 0;
}
Vector3 movement = (direction * movementSpeed) * Time.deltaTime;
_charController.Move(movement);
if (direction != Vector3.zero)
{
gameObject.transform.right = direction;
}
if(groundedPlayer && jumpPressed || groundedPlayer && joybutton.Pressed)
{
Debug.Log("character should jump");
playerVelocity.y += Mathf.Sqrt(jumpForce * jumpSpeed * gravity);
}
playerVelocity.y += gravity * Time.deltaTime;
_charController.Move(playerVelocity * Time.deltaTime);
characterAnimator.SetFloat("Speed", Mathf.Abs(moveInput.x));
}
private void OnEnable()
{
playerInput.Enable();
}
private void OnDisable()
{
playerInput.Disable();
}
}
I can't quite explain what happened, it was working one minute and the next the character was neither moving left or right, and it wasn't being brought down by gravity. I've commented out almost every line and Debug.Logged as much as I can see where I can get a result but I can't quite track down where the issue is coming from. I've looked around a bit on Unity Answers/general searching around but can't seem to find anyone else with a conclusive solution for Character Controllers suddenly "not working". Any one had experience with this?
Update: I've lodged a bug related to this as I haven't got any comments or update related to if it's my actual code or the Character Controller itself.
Your answer
Follow this Question
Related Questions
Problem with the Character Controller on the Y-Axis,Character Controller drifting in the Y Axis 0 Answers
Adding force to direction of a Character Controller? 0 Answers
Character only falls when button is pushed down 0 Answers
Bug on Character movement 0 Answers
Should I use Rigidbodies in a top down 2D game if I don't need physics? 0 Answers