- Home /
How to make player go through object but can't go back
Hi, I'm making a magic wall which allow player to go through but can't go back. How to do this?
Answer by JimmyVegas · Jul 11, 2017 at 01:41 PM
Pretty easy way would be to do this:
Turn off the BoxCollider on the wall.
Place a trigger on the other side of the wall which activates the BoxCollider when you step into it.
Example:
public GameObject theWall;
public void OnTriggerEnter () {
theWall.GetComponent<BoxCollider>().enabled = true;
}
Assuming there would be nothing going through the wall apart from the player, this should work. If you have other things, use a Tag.
sorry but it doesn't work I'm using Java so i tried to understand it and use it but i can't
Answer by JustinEllis · Jul 11, 2017 at 06:19 PM
Well if its just linear movement you could put a box collider on the wal. Mark it as trigger, and then in your player script have this
function OnTriggerExit(other: Collider)
{
if(other.gameObject.tag == "WallTag")
{
other.IsTrigger = false;
}
}
Im not sure if that works or not but it makes sense to me. I usually on use C# so some of the syntax might me wrong. If your project is 2d use OnTriggerExit2. Remember you have to create a new tag a lnd put it in all the walls
Wouldn't this suddenly displace either either object? Or make the player immovable? I'm imagining what happens when a player collides with a collide instantiated right on them like a poorly configured weapon object.
@jwizard93 No Since it is OnCollisionExit. $$anonymous$$eaning when this object gets its collider back it won't be colliding with the player.
Is your object/wall set as a trigger?
Also make sure that youre player has a collider as well that is not marked as trigger. Try using debug statements in your collision to see if its even registering the collision.
it might also be other.collider.isTrigger
Answer by Cornelis-de-Jager · Jul 12, 2017 at 01:45 AM
On your wall object (Ensure the wall has a BoxCollider) add a new script and inside the script add the code below. :
function Start () {
GetComponent<BoxCollider> ().enabled = false;
}
function OnCollisionExit(other: Collision)
{
if(other.gameObject.tag == "Player")
{
GetComponent<BoxCollider> ().enabled = true;
}
}