Home > Net >  Why does a character go up in the air when a character controller is added to it?
Why does a character go up in the air when a character controller is added to it?

Time:11-16

When I add a Character Controller component to my character, my character goes up in the air by 0.08 units. This only happens when I start moving. The collider seems to be in the right position.

Here is a screenshot of the character with the Character Controller component: enter image description here

Screenshot of my character settings:

enter image description here

My movement code:

[SerializeField] private Transform _orientation;

private CharacterController _controller;

private float xInput;
private float yInput;

private Vector3 _moveDirection;

private float rotationSpeed = 10f;
private float speed = 5f;

private void Awake()
{
    _controller = GetComponent<CharacterController>();
}

private void Start()
{
    Cursor.lockState = CursorLockMode.Locked;
}

private void Update()
{
    xInput = Input.GetAxis("Horizontal");
    yInput = Input.GetAxis("Vertical");

    _moveDirection = _orientation.forward * yInput   _orientation.right * xInput;

    RotatePlayerToCameraView();
    Move(_moveDirection);
}

private void RotatePlayerToCameraView()
{
    Vector3 viewDirection = transform.position - new Vector3(_orientation.position.x, transform.position.y, _orientation.position.z);
    _orientation.forward = viewDirection.normalized; // понять зачем это и как вообще

    gameObject.transform.forward = Vector3.Slerp(gameObject.transform.forward, viewDirection.normalized, Time.deltaTime * rotationSpeed);
}

private void Move(Vector3 moveDirection)
{
    float scaledMoveSpeed = speed * Time.deltaTime;

    moveDirection = new Vector3(moveDirection.x, 0, moveDirection.z);

    _controller.Move(moveDirection * scaledMoveSpeed);
}

CodePudding user response:

There appears to be correlation between the Character Controller's skin width: 0.08 and the issue of my character goes up in the air by 0.08 units.

Take a look at the CharacterController.skinWidth Unity docs:

Specifies a skin around the character within which contacts will be generated by the physics engine.

It appears the physics engine is using the width for the overall contacts.

  • Related