OnTriggerEnter not working
Hello everyone,
I am trying to have the player be able to walk through a wall (game object) that is set inactive on Awake and then once they walk through it (activating the tigger), I want it to set the Box Collider (attached to the game object) active so they cannot go back through the same wall they just went through.
Here is the script I have
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;
}
}
}
I was going to try "Destroy(other.gameObject);" to make it so the player wouldn't be able to go back through as the wall has the box collider on it and is set as "is trigger". But that is a side note, as it is not even setting the Box Collider active after the player goes through the trigger.
At Awake the level will set the wall inactive as it should but when the player walks through, it does nothing.
Any help is very welcomed, this seemed as an easy thing to do, and it has been a nightmare to figure out.
Answer by SlowCircuit · Jun 13, 2018 at 12:57 AM
Make the wall's collider default to isTrigger being true. The in OnTriggerExit, set the collider isTrigger to false so it's a normal collider. By having it in enter, the wall would turn solid with the player still inside it.
You may also want to have it record the player's position in OnTriggerEnter and compare it with their position on exit to make sure they're exiting from the other side. Otherwise players might walk partly in, walk backwards and then be stuck behind a solid wall.
At least one of the objects needs a rigidbody and it can NOT be set to kinematic.
On the Physics settings layer matrix, the box where their layers intersect MUST be checked.
If you're using trigger settings like I'm suggesting, make sure the wall collider and object are both enabled.
that makes sense...but how do i set isTrigger to false in OnTriggerExit?
I don't see a layer matrix, i see "tags and layers" in which nothing is there because i have not done any layers.
thank you for the help, i just need a little more please.
I am just going to post a new question with a video of what is happening.
Edit > Project Settings > Physics
That's where you edit the layer matrix. If you're doing a 2D game then it's Physics 2D of course.
void OnTriggerExit() { col_wall.isTrigger = false; }
Answer by Tobychappell · Jun 12, 2018 at 09:27 PM
So if I understand this correctly. The wall GameObject has a trigger collider and this script attached to it. and this wall GameObject is active.
And this GameObject has an active child that has a non-trigger collider on it and is referenced in the script as col_wall. On Awake this col_wall is disabled by this script.
The logic seems sound. It may be a configuration issue.
Is col_wall definitely set to be this child box collider and not the trigger collider. Do you noticed the trigger collider become disabled?
Does the player have a rigidbody?
Are they on the same physics layer? Or on layers that are set to interact with each other?
Might want to change your title for this question as you are using OnTriggerEnter in your code not OnCollisionEnter
Try looking at this example project:
I am not sure I understand what you mean? The box collider is a child of the wall. the wall (game object) is set as "is trigger" and has the script attached to it. are you saying i should have a collider separate from the wall ?
Ahhh right, sorry, when you said 'the box collider' I didn't know this was the physical collider and that it was childed to the wall GameObject.
So if I understand this correctly. The wall GameObject has a trigger collider and this script attached to it. and this wall GameObject is active.
And this GameObject has an active child that has a non-trigger collider on it and is referenced in the script as col_wall. On Awake this col_wall is disabled by this script.
The logic seems sound. It may be a configuration issue.
Is col_wall definitely set to be this child box collider and not the trigger collider. Do you noticed the trigger collider become disabled?
Does the player have a rigidbody?
Are they on the same physics layer? Or on layers that are set to interact with each other?
$$anonymous$$ight want to change your title for this question as you are using OnTriggerEnter in your code not OnCollisionEnter.
fixed the title.
i do see the wall get set inactive. it just does not get set active when the player walks through the box collider.
The player does have a rigidbody - i even tied just using the pre-built first person controller that unity provides with the standard assets and it did not work.
not sure what you are talking about with the layers, perhaps that is the issue if the logic is all there. ill look at that, is that really the only thing that could be the issue ?
Follow this Question
Related Questions
On trigger enter help 1 Answer
How to initiate something before Application.LoadLevel? (C#) 0 Answers
My Game Over Canvas is working in one level but not the other! 0 Answers
Unable to reactivate an object that's been deactivated. C# 4 Answers
SetActive delay between code and actual object on the screen 0 Answers