【问题标题】:Player's Bumpy Movement球员的颠簸运动
【发布时间】:2020-04-08 01:14:01
【问题描述】:

当我的玩家跳跃并落在某物上时,它总是在 like that. 周围晃动 此外,有时当我连续跳几次(大约 10 次)时,它会穿过地面然后掉下来。 我尝试使用对撞机,但在运动脚本中没有看到任何可能导致此问题的内容。我目前正在使用 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;

    }
}

【问题讨论】:

  • 您是否尝试过通过physicsMaterial 禁用刚体的弹跳(以防它确实是刚体弹跳,可能不是)?
  • 刚做了一个弹力为0的新材料试一试,还是摇摇晃晃的。

标签: c# visual-studio unity3d


【解决方案1】:

编辑:我没有注意到链接。

你强迫它下降到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)

在它之后。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    相关资源
    最近更新 更多