- Home /
Help with OnTriggerEnter issue
I want the player to walk through a wall then then not be able go though it again. So, i am using OnTriggerEnter on a box collider attached to the wall (gameobject) to have this work. everything is on the same layer, the player has a rigidbody, i am not sure why this is not working.
Here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetWallInactive : MonoBehaviour
{
[SerializeField] BoxCollider col_wall;
void Awake()
{
col_wall.enabled = false;
}
void OnTriggerEnter(Collider col)
{
if (col.tag == "Player")
{
col_wall.enabled = true;
}
}
}
here is a video of what is happening....not sure why it isn't working. Problem video
any help is welcomed, this has been very difficult to try and figure out. Please let me know if you need to know anything else. @thetomfinch @Tobychappell @Feelnside
I'm very much a newbie, but if the collider is set to inactive, can it read when something collides with it?
Answer by SlowCircuit · Jun 13, 2018 at 03:44 AM
Does your player have a collider on them? If not there's nothing for the wall to collide with.
I know for sure one reason why it's not working. In the other question/answer I said that you need the box collider to be ON. Take the part that disables it out of your code. isTrigger means the collider doesn't block the player, only triggers when they enter/stay/exit it. If the box collider is off, then it's not doing anything so it wouldn't ever trigger.
Yes the player has a ridgidbody and a mesh collider. i change the code to this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetWallInactive : $$anonymous$$onoBehaviour
{
[SerializeField] BoxCollider col_wall;
void Awake()
{
col_wall.enabled = true;
}
void OnTriggerEnter(Collider col)
{
if (col.tag == "Player")
{
col_wall.enabled = false;
}
}
private void OnTriggerExit(Collider col)
{
if (col.tag == "Player")
{
col_wall.enabled = false;
}
}
}
so, now it is active when the game is started, then the player goes through the collider and its set inactive and i am able to go though it, but it does not set it back active so its still the same issue, is it an issue with the OnTriggerExit?
ok, so I have tried this code,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetWallInactive : $$anonymous$$onoBehaviour
{
[SerializeField] BoxCollider col_wall;
void Awake()
{
col_wall.enabled = true;
col_wall.isTrigger = true;
}
void OnTriggerEnter(Collider col)
{
if (col.tag == "Player")
{
col_wall.enabled = false;
}
}
private void OnTriggerExit(Collider col)
{
if (col.tag == "Player")
{
col_wall.enabled = true;
col_wall.isTrigger = false;
}
}
}
It still is not having the desired effect, the wall box collider is active on start, then then the player walks through the box collider it becomes inactive and the player can go though it, but then it does not set the box collider active again. I tired to use Update() to check if the player has gone though the box collider and if so set it enabled but that did not work. any ideas ?
Listen very, very closely cause you keep missing the most important part.
Remove all code from this script that uses .enabled. You want the collider to ALWAYS be enabled. Replace it with col_wall.isTrigger = false in OnTriggerExit(). You do not want any disabling/enabling code in OnTriggerEnter. $$anonymous$$ake sure that the collider in scene is enabled and isTrigger is true. You don't need to set anything in awake because it's already set in inspector.
Your code should look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetWallInactive : $$anonymous$$onoBehaviour
{
private void OnTriggerExit(Collider col)
{
if (col.tag == "Player")
{
col_wall.isTrigger = false;
}
}
}
I meant to remove the awake method from the script I gave them... dangit
Answer by crawniik · Jun 13, 2018 at 06:02 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetWallInactive : MonoBehaviour
{
[SerializeField] BoxCollider col_wall;
private void OnTriggerExit(Collider col)
{
if (col.tag == "Player")
{
col_wall.isTrigger = false;
}
}
}
This should do what you want. It's working on my side.
Edit: Make sure the wall we're talking about has the Is Trigger option checked first though.
I will check it, thank you for working with me on this issue.
I honestly can't even take the credit. I only figured that out after reading @ZanyT 's answer.
If it still isn't working for you, make sure you put some kind of collider on your player. You can use mesh collider if it's not a basic shape like a sphere/capsule/cube etc.
Answer by ZanyT · Jun 13, 2018 at 03:34 AM
The player needs to have a collider.
In order for the wall to stop the player from walking through it, the collider needs to not be a trigger.
You might run into issues with OnTriggerEnter blocking the player immediately: if so, change it to OnTriggerExit.
My suggestion would be to start with the player having a non-trigger collider, and having the wall with an is-trigger collider. You need both colliders enabled. Then, in OnTiggerExit, set the walls collider isTrigger to false in the code, and the two non-trigger colliders should prevent the player from going back through the wall.
With the way you have it right now, with a disabled collider, it's as if that component isn't on the object at all, so it has no way to detect collision as it is.
the player does not have a is trigger on it, only the wall does. and how would i set the walls collider isTrigger to false in the code? also if i have the collider active the player would not be able to walk through the wall, so would i have to set the wall collider inactive when the player enters the collider, and then set it active when they exit?
A collider that is set to "Is Trigger" does not actually collide with anything. It just detects things that go through it. In the code, let's say that your collider is called "col" you would say: col.isTrigger = true; Essentially ins$$anonymous$$d of enabled and disabled the collider, you need to change the isTrigger between true and false. And both the player and wall need a collider.