Ухабистое движение игрока - PullRequest
0 голосов
/ 05 апреля 2020

Когда мой игрок прыгает и приземляется на что-то, он всегда так трясется . Кроме того, иногда, когда я прыгаю несколько раз подряд (примерно 10 раз), он go проникает сквозь землю и просто упасть Я пытался использовать коллайдер, и я не вижу в скрипте движения ничего, что могло бы вызвать это. В настоящее время я использую Unity 2018.4.20f1 и Visual Studio c#. Любые решения?

Скрипт движения игрока:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public static int movespeed = 6;
    public Vector3 userDirection = Vector3.right;

    public Rigidbody rb;

    public bool isGrounded;

    Vector3 jumpMovement;

    bool jump = false;

    [SerializeField]
    float jumpHeight = 1.8f, jumpSpeed = 8f;

    void Start()
    {
        rb = GetComponent<Rigidbody>();    }


    void OnCollisionStay()
    {
        isGrounded = true;
    }

    public float fallMultiplier = 3.5f;
    public float lowJumpMultiplier = 2f;


    void Update()
    {
        //movement
        transform.Translate(userDirection * movespeed * Time.deltaTime);

        if (Input.GetButton("Jump") && !jump)
            StartCoroutine(Jump());
    }


    IEnumerator Jump()
    {
        float originalHeight = transform.position.y;
        float maxHeight = originalHeight + jumpHeight;
        jump = true;
        yield return null;

        while (transform.position.y < maxHeight)
        {
            transform.position += transform.up * Time.deltaTime * jumpSpeed;
            yield return null;
        }

        while (transform.position.y > originalHeight)
        {
            transform.position -= transform.up * Time.deltaTime * jumpSpeed;
            yield return null;
        }

        rb.useGravity = true;
        jump = false;

        yield return null;

    }
}

1 Ответ

0 голосов
/ 07 апреля 2020

Редактировать: я не заметил ссылку.

Вы увеличиваете ее с go до originalHeight, не проверяя, есть ли объект выше этого. Вы можете удалить

while (transform.position.y > originalHeight)
{
    transform.position -= transform.up * Time.deltaTime * jumpSpeed;
    yield return null;
}

и позволить твердому телу контролировать гравитацию (установив rb.useGravity = true;, как вы сделали).

Исходный ответ:

Возможно, вы идете ниже земля при звонке transform.position -= transform.up * Time.deltaTime * jumpSpeed;. Попробуйте добавить

if (transform.position.y < originalHeight)
    transform.position = new Vector3(transform.position.x, originalHeight, transform.position.z)

после него.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...