Как убрать дёрганье персонажа при попытке запрыгнуть на уступ?

Я сделал персонажа с управлением от первого лица. Но так получается, что если продолжать идти в коробку и прыгнуть, то персонажа начинает дёргать, когда он достигает серединой своего тела верхней части коробки и в итоге он не допрыгивает и падает. Если не сходить с того места и нажать кнопку прыжка без движения, то персонаж прыгнет нормально. Знает кто-нибудь как решить эту проблему?

Вот код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public float sphereRadius = 0.1f;
    private AudioSource m_AudioSourse;

    private Vector3 velocity;

    public Transform groundChecker;
    public Transform ceilingChecker;
    public LayerMask ground;

    public AudioClip[] step;

    bool isGrounded;
    bool isCeiling;
    bool inWater;
    private void Start()
    {
        m_AudioSourse = GetComponent<AudioSource>();
    }
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundChecker.position, sphereRadius, ground);
        isCeiling = Physics.CheckSphere(ceilingChecker.position, sphereRadius, ground);
        if(isCeiling)
        {
            velocity.y = -2f;
        }

        if(isGrounded && velocity.y < 0)
        {
            velocity.y = -4f;
        }

        if(Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        if(x!=0||z!=0)
        {
            PlayStepSound();
        }

        Vector3 move = transform.right * x + transform.forward * z;

        if (Input.GetButton("Run"))
        {
            move *= 1.5f;
        }

        controller.Move(move * speed * Time.deltaTime);

        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
        Debug.Log(velocity.y);
    }
    void PlayStepSound()
    {
        float pitchVal;
        if (Input.GetButton("Run"))
            pitchVal = 1.5f;
        else
            pitchVal = 1f;
        if (!m_AudioSourse.isPlaying && isGrounded)
        {
            int n = Random.Range(0, step.Length);
            m_AudioSourse.clip = step[n];
            m_AudioSourse.pitch = pitchVal;
            m_AudioSourse.Play();
        }
    }
}
  • Вопрос задан
  • 72 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы