我在联合学院做一个人工智能“迷你游戏”。我试图让简单的特工在一个可行走的网格内移动使用痛苦。现在我使用的是蜂群转向,我使用的是这篇文章中显示的方法。
我遇到的问题是,正如您在图像中看到的那样,OverlapSphere函数没有正确返回对撞机:
这是我用来驾驶的密码。重叠球调用中的8是用于此转向的对撞机所在的层。
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正确地返回其内部对撞机的数量。
发布于 2019-11-01 18:38:12
重叠球调用中的8是用于此转向的对撞机所在的层。
8
和第8层的位掩码不是一回事。
8
就是8
。第8层的位掩码等于1<<8
或256个。
此外,您的角色似乎不是在第8层,而是在第0层,因此当然您的OverlapSphere将返回零对撞机(除非子层位于另一层,屏幕截图并没有说明这一点)。
你会想要这样的:
Collider[] agents_near = Physics.OverlapSphere(transform.position, detection_radius, 1<<8);
除了将您的对象更改为实际位于第8层之外(虽然我会选择另一个对象,因为Unity有硬编码的第8层的名称为Post Processing
,它没有您想要的上下文值)。
或
Collider[] agents_near = Physics.OverlapSphere(transform.position, detection_radius, 1<<0); //cast against layer 0.
见:层。
https://stackoverflow.com/questions/58663058
复制相似问题