On Trigger Enter not working RTS

Hello, I’m trying to use an OnTriggerEnter to build a list for the units, working on having an attack at will so they will auto-attack the closet unit, but it’s not working.

In the RPG project, I have the same code and setup and it works fine, was wondering if it had anything to do with the networking?

This is on a child object on the Unit and takes in the Unit’s Targeter.

public class TargetCollider :  MonoBehaviour

{

    Collider trigger;

    [SerializeField] Targeter targeter = null;

    void Start()

    {

        trigger = GetComponent<Collider>();

        if (trigger)

        {

            trigger.isTrigger = true;

        }

    }

    private void OnTriggerEnter(Collider other)

    {

        targeter.OnUnitEnter(other);

    }

    private void OnTriggerStay(Collider other)

    {

        targeter.OnUnitStay(other);

    }

    private void OnTriggerExit(Collider other)

    {

        targeter.OnUnitExit(other);

    }

}

This is in the Targeter script.

public void OnUnitEnter(Collider other)

    {

        if(other.TryGetComponent<Targetable>(out Targetable target))

        {

            if (target.hasAuthority)

            {
               return;
            }

            attackTargets.Add(target.gameObject);

        }

    }

    public void OnUnitStay(Collider other)

    {

        float distanceToClosestTarget = Mathf.Infinity;

        List<GameObject> deadTargets = new List<GameObject>();

        foreach (GameObject target in attackTargets)

        {

            float distancetoTarget = (target.transform.position - this.transform.position).sqrMagnitude;

            Health health = target.GetComponent<Health>();

            Targetable tar = target.GetComponent<Targetable>();

            if (health.IsDead())

            {

                deadTargets.Add(other.gameObject);

            }

            if (attackTargets.Count != 0 && attackAtWill == true && targetAble == null)

            {

                if (distancetoTarget < distanceToClosestTarget)

                {

                    distanceToClosestTarget = distancetoTarget;

                    targetAble = tar;

                }

            }

        }

        foreach (GameObject dead in deadTargets) //This section does the removing outside of the loop

        {

            attackTargets.Remove(dead);

        }

        return;         

    }

    public void OnUnitExit(Collider other)

    {

       attackTargets.Remove(other.gameObject);

    }

It works in my RPG project

Hey there, are you able to confirm that they triggered the collider? If you serialize the list are you able to see the targets populate the list?

No they are not triggering the collider, they are not populated in the list.

Is this script on the same gameObject as the collider? I would try and drop in a a debug.log and try and figure out if your script has hold of the right collider, and from there see if you can figure out why it’s not getting triggered.

The script is on the child object that has a capsule collider, the right collider is being used, it’s not " is trigger " by default and is made trigger in Start. Have a Dbug.log in Start and onTriggerEnter, Start debug log prints but not the OnTriggerEnter.

Do the units have rigidbody’s on them?

Yes.

Well, I can’t see a networking issue that would cause this. All the collisions are happening locally on the client. The only thing I could think of is if the server is refreshing the transform infrequently enough that the unit “jumps” over the boundary of the collider. Is OnTriggerStay() being triggered?

Nope, I tried removing the collider from the child object and using the one on the parent, that didn’t work.
Second I passed in a target Unit in the list to see if the OnTriggerStay foreach loop would work and it doesn’t even if there is a Unit in the list.

I was thinking maybe I had to do an OnSeverStart for the child object script, but making it a NetworkBehaviour caused it to have a networkID and I got errors for there being two on the parent object.

Also just tested having the Targeter take in the child object collider, this is in the OnServerStart

        if (col)
        {
            col.isTrigger = true;
        }

it works changing the child collider to a trigger but no collisions.

So was doing some more brainstorming wondering why the projectile works, but not this.

So I tried to Instantiate the child prefab object the same way as the projectile that’s network spawned, it works, the prefab is on the unit and the targeter still takes in the prefab collider.

This is in the targeter script

[ServerCallback]

    private void OnTriggerEnter(Collider other)

    {

        Debug.Log("Enter");

        if (other.tag == "Unit")

        {

            if (other.TryGetComponent<NetworkIdentity>(out NetworkIdentity networkIdentity))

            {

                if(networkIdentity.connectionToClient == connectionToClient) {return;}

            }

            if(other.TryGetComponent<Targetable>(out Targetable target))

            {

                attackTargets.Add(target.gameObject);

            }

        }

    }

The funny thing is the debug.log (Enter) is called when a projectile triggers it but the units don’t.
I don’t know why that is lol.

I have noticed that the Debug is called when the projectile hits the unit, I’m assuming that even tho I have the child gameobject’s collider referenced, the OnTriggerEnter is using the collider on the Parent gameobject

[SerializeField] private Collider col = null;

Tested this increasing the radius of the parent capsule collider on one unit, when a unit came close no trigger, but then I made one of the unit’s parent collider a trigger and it worked, still working it.

1 Like

Think I have it now, it was the layer of the child collider. I had it change from default to another layer because the default blocked the raycast keeping me from clicking on the units, We changed the physics for all the layers so the other layers were not colliding and some blocked the raycast.

Making a new layer and checking the physics in the project settings should fix it, thanks for your patience :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms