简体   繁体   中英

Physics.OverlapSphere not detecting colliders correctly

I'm doing an AI "minigame" in Unity for a College Assigment. I'm trying to make simple agents moving inside a walkable mesh using steerings. Right now I'm with the Flocking steering and I'm using the method showed in this article .

The problem I'm having is that the OverlapSphere function doesn't return the colliders correctly as you can see in the image:

场景

Here's the code I'm using for the steering. The 8 in the Overlap Sphere call is the layer where the colliders used for this steering are located.

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);
    }
}

The expected result is the OverlapSphere returning correctly the number of colliders inside it.

The 8 in the Overlap Sphere call is the layer where the colliders used for this steering are located.

8 and a bitmask of Layer #8 are not the same thing.

8 is just 8 . A bitmask of layer #8 is equal to 1<<8 or 256.

Additionally, it does not appear that your characters are not on layer 8, but on layer 0, so of course your OverlapSphere is going to return zero colliders (unless the children are on a different layer, the screenshot did not make that clear).

You'll want this instead:

Collider[] agents_near = Physics.OverlapSphere(transform.position, detection_radius, 1<<8);

As well as changing your object to actually be on Layer 8 (though I would pick a different one, as Unity has hard-coded layer 8's name as Post Processing which does not have the contextual value you want for your purposes).

OR

Collider[] agents_near = Physics.OverlapSphere(transform.position, detection_radius, 1<<0); //cast against layer 0.

See: Layers .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM