Other
Help with trigger detection?
Hi there. I'm trying to have a trigger detect if a ball is in it's trigger zone. The values don't seem to be changing as they should accordingly. It is on UNET but only the local player needs to know if it is triggered or not. I mention that it's on UNET just in case something related to it is causing the issue. If the issue doesn't stem from UNET, I think it maybe that I'm not referencing the box collider properly. The trigger checkbox is checked on it. The way I'm doing it is having a script attached to the gameobject with the trigger collider, and then having my playercontroller script reference if it is triggered or not, then setting a float number to 1 if it is triggered, 0 if it's not. I was trying to see if [Command] would work for the OnTriggerEnter() and OnTriggerExit() voids but I couldn't get it working(I probably just coded it incorrectly if that is where it is going wrong).
Here's the script that is attached to the object with the collider:
///////////////////////////////////////////
public static float Triggered;
void OnTriggerEnter(Collider Trigger)
{
if (Trigger.gameObject.CompareTag("Ball"))
{
Triggered = 1;
}
else
{
Triggered = 0;
}
}
void OnTriggerExit()
{
Triggered = 0;
}
///////////////////////////////////////////
Here's the Reference to it in my playercontroller script:
//HitboxDetection
if (Hitbox.Triggered == 1)
{
IsTriggered = 1;
}
else if (Hitbox.Triggered == 0)
{
IsTriggered = 0;
}
if (IsTriggered == 1)
{
Debug.Log("CanPass");
}
Does anyone have an idea of what I'm doing wrong? Thanks in advance.
Comparing floats can be tricky so you should use a bool or int ins$$anonymous$$d.
I switched it to an int but it still isn't working.
you should check if you even get the trigger calls correctly and then take the networking step, one at a time.
Answer by michelle12188 · Dec 11, 2016 at 11:18 PM
Do you have a rigidbody on either the trigger or the ball? If not add one.
from the unity docs:
Trigger events are only sent if one of the Colliders also has a Rigidbody attached.
Is it not recognizing the trigger function at all or is it never recognizing the tag? A thing to note as well is that a default for an int is 0 so you won't see a change in the triggered if it is going to the else statement/OnTriggerExit(). I would suggest a few debugs so you can tell in the Console while you're trying to solve it.
I used debut.logs in both the OnTriggerEnter void and in the if statement that's in the OnTriggerEnter void. The only time either showed up in the console was as soon as I started a server and it was only the one for the void, not the if statement. I tried having multiple objects enter the trigger but still nothing. So that tells me it's the OnTriggerEnter, not the tag. One thing to consider is that the collider trigger and the script are on a child object of my playerprefab. $$anonymous$$aybe it can't access it? Is there a way to assign the trigger via a public Collider then just drag it onto the slot. $$anonymous$$aybe that will work.
Here's the the script again:
///////////////////////////////////////////
public static int Triggered;
void OnTriggerEnter(Collider Trigger)
{
Debug.Log("TriggerDetectsObject");
if (Trigger.gameObject.CompareTag("Ball"))
{
Debug.Log("TriggerDetectsBall");
Triggered = 1;
}
else
{
Triggered = 0;
}
}
void OnTriggerExit()
{
Triggered = 0;
}
///////////////////////////////////////////