首页 > 解决方案 > 我试图将代码从 2d (Physics.OverlapCircle) 转换为 3d (Physics.OverlapSphere) 但它仍然不起作用,有人可以帮助我吗?谢谢

问题描述

我是编程新手,我尝试将代码从 2d 转换为 3d(从 Physics.Overlapcircle 转换为 Physics.Overlapsphere),但它仍然不起作用。我还在 Stack Overflow 上寻找了问题,但是有了这个提示,它也不起作用……我为基于网格的游戏制作了控制器……一切正常,但我可以穿过障碍物。用 Overlap Sphere 制作它是错误的吗?你有别的想法吗?谢谢这里是代码:

public class MovementPlayer1 : MonoBehaviour
{
    public float moveSpeed = 5f;
    public Transform movePoint;
    public Transform forward, back, left, right;
    public Collider[] colliders;

    public LayerMask whatStopsMovement;

    void Start()
    {
        movePoint.parent = null;
    }

    void Update()
    {
        transform.position = Vector3.MoveTowards(transform.position, movePoint.position, moveSpeed * Time.deltaTime); //Time.deltaTime, damit es überall gleich schnell ist

        if (Vector3.Distance(transform.position, movePoint.position) <= .05f)
        {
            if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) == 1f) //Mathf.Abs fragt, ob es entweder positiv oder negativ ist, bzw. falls Input.GetAxis... sich irgendwie bewegt
            {
                colliders = Physics.OverlapSphere(movePoint.position + new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0), 0.2f, whatStopsMovement);
                if (colliders != null)
                {
                    movePoint.position += new Vector3(Input.GetAxisRaw("Horizontal"), 0, 0);
                }
            }

            if (Mathf. Abs(Input.GetAxisRaw("Vertical")) == 1f)
            {
                colliders = Physics.OverlapSphere(movePoint.position + new Vector3(0, 0, Input.GetAxisRaw("Vertical")), 0.2f, whatStopsMovement);
                if (colliders != null)
                {
                    movePoint.position += new Vector3(0, 0, Input.GetAxisRaw("Vertical"));
                }
            }
        }
    }  
}

标签: c#unity3d

解决方案


我用 SweepTest 做到了,它奏效了。我不知道为什么它不适用于 Physics.OverlapSphere。对于发现这个问题的人,我建议你使用 SweepTest 来做。感谢大家的帮助!


推荐阅读