【问题标题】:Colliders in unity falling through objecs统一的对撞机从物体中落下
【发布时间】:2021-09-06 06:37:39
【问题描述】:

所以基本上我统一创建了一个简单的 3d 场景,但我的“角色”不断从物体中跌落或穿过它们,我尝试了每种类型的对撞机(网格、盒子、地形),甚至尝试为每个物体添加刚体组件,但是它没有成功

这是我让“角色”移动的代码


public class PlayerMovement : MonoBehaviour
{
    //
    public CharacterController controller;

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

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;
    

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

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

        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

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

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

        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
        
        if(Input.GetKey(KeyCode.LeftShift))
        {
            speed = 24;
        }
        else
        {
            speed = 12;
        }

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

    }
}

【问题讨论】:

  • 是否有任何对撞机标记为IsTrigger?你有没有检查物理设置 -> 层碰撞矩阵?也许你的图层会互相忽略?

标签: unity3d


【解决方案1】:

首先,您直接使用变换位置操作的任何内容都将通过其他对象,但我可以看到您没有这样做 - 至少在这段代码中。您正确地要求角色控制器移动,它看起来类似于此处 Unity 文档中给出的示例:https://docs.unity3d.com/ScriptReference/CharacterController.Move.html 以下是角色控制器如何工作的详细说明: https://medium.com/ironequal/unity-character-controller-vs-rigidbody-a1e243591483

基本碰撞器是最好的开始,但角色控制器已经有一个胶囊碰撞器。通常,如果您正在编写自己的自定义控制器和运动代码,您会为玩家添加一个刚体,这将满足一个对撞机必须有一个刚体以使物理影响它并导致碰撞的要求。检查项目设置中的物理矩阵是一个很好的起点,但如果您刚刚创建了一个新项目,那么我怀疑这就是问题所在。确保您的地面/地形具有定义平坦地面的箱形对撞机。

我的建议是使用 Unity 第三人称控制器作为起点: https://assetstore.unity.com/packages/essentials/starter-assets-third-person-character-controller-196526

关于什么碰撞器将相互碰撞以及何时需要刚体的完整说明在这里:https://docs.unity3d.com/Manual/CollidersOverview.html

【讨论】:

    【解决方案2】:

    很可能,由于角色是一个复杂的网格,您选择了 Mesh Collider 并且忘记选中凸面复选框...自 Unity 5 起需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-08
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多