c# - 我试图将代码从 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"));
}
}
}
}
}
解决方案
我用 SweepTest 做到了,它奏效了。我不知道为什么它不适用于 Physics.OverlapSphere。对于发现这个问题的人,我建议你使用 SweepTest 来做。感谢大家的帮助!
推荐阅读
- javascript - 使用 js-cookies,如何轻松将对象设置为 cookie?
- swiftui - 如何通过使用 @EnvironmentObject 来路由视图来导航到项目的详细视图?
- python - werkzeug.exceptions.BadRequestKeyError
- javascript - 如何编写一个接受任何输入的 Typescript hasOwnProperty 类型保护?
- xml - 在基于注释的方法中如何实现松散耦合
- javascript - 如何在不弄乱唯一 ID 的情况下克隆输入字段?
- python - 按字母顺序对序列进行排序-Python
- dart - 我试图在飞镖垫,在线平台上运行代码
- c# - 我的游戏在过渡完成之前加载 Unity 我该如何解决这个问题?
- linux - 如果文件可使用 s3 命令公开访问,如何显示?