0
\$\begingroup\$

When connecting a Character Controller component to the character in Unity, it starts to rotate uncontrollably. Without the Character Controller, everything works fine, but the character does not walk. What is the problem?

using System;
using UnityEngine;

public class CharaterController: MonoBehaviour
{
     private float _rotateSpeed = 75;
     private float _runSpeed = 8;
     private float _walkSpeed = 5;

     private Camera _playerCamera;
     private CharacterController _characterController;

     private Vector2 _rotation;
     private Vector3 _velocity;
     private Vector2 _direcrion;

     private void Start()
     {
        _characterController = GetComponent<CharacterController>();
        _playerCamera = GetComponentInChildren<Camera>();
        Cursor.lockState = CursorLockMode.Locked;
     }

     private void Update()
     {
        _characterController.Move(_velocity * Time.deltaTime);

        _direcrion = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));

        mouseDelta *= _rotateSpeed * Time.deltaTime;
        _rotation.y += mouseDelta.x;
        _rotation.x = Mathf.Clamp(_rotation.x - mouseDelta.y, -90, 90);
        _playerCamera.transform.localEulerAngles = _rotation;
     }
     private void FixedUpdate()
     {
        _direcrion *= Input.GetKey(KeyCode.LeftShift) ? _runSpeed : _walkSpeed;
        Vector3 move = Quaternion.Euler(0, _playerCamera.transform.eulerAngles.y, 0) * new Vector3(_direcrion.x, 0, _direcrion.y);
        _velocity = new Vector3(move.x, _velocity.y, move.z);
     }
}
\$\endgroup\$
0

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.