首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Physics.OverlapSphere未正确检测对撞机

Physics.OverlapSphere未正确检测对撞机
EN

Stack Overflow用户
提问于 2019-11-01 17:00:19
回答 1查看 4.4K关注 0票数 0

我在联合学院做一个人工智能“迷你游戏”。我试图让简单的特工在一个可行走的网格内移动使用痛苦。现在我使用的是蜂群转向,我使用的是这篇文章中显示的方法。

我遇到的问题是,正如您在图像中看到的那样,OverlapSphere函数没有正确返回对撞机:

这是我用来驾驶的密码。重叠球调用中的8是用于此转向的对撞机所在的层。

代码语言:javascript
运行
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SteeringFlocking : SteeringAbstract
{
    Move move;
    public uint detection_radius = 5;

    // Start is called before the first frame update
    void Start()
    {
        move = GetComponent<Move>();
    }

    // Update is called once per frame
    void Update()
    {
        Collider[] agents_near = Physics.OverlapSphere(transform.position, detection_radius, 8);

        if (agents_near.Length > 0)
        {
            Vector3 final = Alignment(agents_near) + Cohesion(agents_near) + Separation(agents_near);
            move.AccelerateMovement(final.normalized * move.max_mov_acceleration, priority);
        }

    }

    private Vector3 Alignment(Collider[] agents_near)
    {
        Vector3 result = Vector3.zero;

        for (uint i = 0; i < agents_near.Length; ++i)
        {
            result += agents_near[i].gameObject.GetComponent<Move>().movement;
        }

        result.x /= agents_near.Length;
        result.y /= agents_near.Length;
        result.z /= agents_near.Length;
        result.Normalize();

        return result;
    }

    private Vector3 Cohesion(Collider[] agents_near)
    {
        Vector3 result = Vector3.zero;

        for (uint i = 0; i < agents_near.Length; ++i)
        {
            result += agents_near[i].gameObject.transform.position;
        }

        result.x /= agents_near.Length;
        result.y /= agents_near.Length;
        result.z /= agents_near.Length;
        result = new Vector3(result.x - move.transform.position.x, result.y - move.transform.position.y, result.z - move.transform.position.z);
        result.Normalize();

        return result;
    }

    private Vector3 Separation(Collider[] agents_near)
    {
        Vector3 result = Vector3.zero;

        for (uint i = 0; i < agents_near.Length; ++i)
        {
            result += agents_near[i].gameObject.transform.position - move.transform.position;
        }

        result.x /= agents_near.Length;
        result.y /= agents_near.Length;
        result.z /= agents_near.Length;
        result *= -1;
        result.Normalize();

        return result;
    }

    private void OnDrawGizmos()
    {
        Gizmos.color = Color.yellow;
        Gizmos.DrawWireSphere(transform.position, detection_radius);
    }
}

预期的结果是OverlapSphere正确地返回其内部对撞机的数量。

EN

回答 1

Stack Overflow用户

发布于 2019-11-01 18:38:12

重叠球调用中的8是用于此转向的对撞机所在的层。

8和第8层的位掩码不是一回事。

8就是8。第8层的位掩码等于1<<8或256个。

此外,您的角色似乎不是在第8层,而是在第0层,因此当然您的OverlapSphere将返回零对撞机(除非子层位于另一层,屏幕截图并没有说明这一点)。

你会想要这样的:

代码语言:javascript
运行
复制
Collider[] agents_near = Physics.OverlapSphere(transform.position, detection_radius, 1<<8);

除了将您的对象更改为实际位于第8层之外(虽然我会选择另一个对象,因为Unity有硬编码的第8层的名称为Post Processing,它没有您想要的上下文值)。

代码语言:javascript
运行
复制
Collider[] agents_near = Physics.OverlapSphere(transform.position, detection_radius, 1<<0); //cast against layer 0.

见:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58663058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档