- Home /
Help with collision detection with specific colliders
Okay, so here's the deal. I want to create some wall running scripts. The way I want to go about it is to have a trigger cube parented to my fps controller in the front, and one on each side. If the front cube collides with a trigger object with the tag of "wall" then the player moves up. If the side cubes collide with the "wall" then run forward. The side triggers and code aren't here yet because i have a problem. The collision seems to be happening with the fps controller, not the trigger cube. If i run into the wall, no matter what direction i'm facing, the wall triggers a hit. I want it to work so that if i face away from the wall, there is no collision. Only when the trigger cube, (which again is positioned just in front of my character, parented to the fps controller), touches the wall should there be collision.
Here is the code attached to the trigger cube:
#pragma strict
var playerState : String = "NoWall";
var character : GameObject;
function Start () {
character = GameObject.FindWithTag("Player");
}
function Update () {
var motorScript = transform.GetComponent(CharacterMotor);
if(playerState == "Wall" && CharacterMotor.grounded == false && Input.GetKeyDown("space"))
{
Debug.Log("Wall Run");
playerState = "WallRun";
character.transform.Translate(0, 10, 0);
}
if(playerState == "WallRun" && CharacterMotor.grounded == true)
{
playerState = "NoWall";
Debug.Log("Nowall");
}
Debug.Log(playerState);
}
function OnTriggerEnter(collisionInfo : Collider)
{
if(collisionInfo.gameObject.tag == "wall")
{
Debug.Log("Wall Hit");
playerState = "Wall";
}
}
function OnTriggerExit(collisionInfo : Collider)
{
if(collisionInfo.gameObject.tag == "wall")
{
Debug.Log("Wall Leave");
playerState = "NoWall";
}
}
So again, i have a wall with normal collision. I have a duplicate wall with Is Trigger selected and with a tag of "wall". I have a cube, parented to the FPS Controller, set to Is Trigger. I have tried doing a Raycast, but I'm not having any luck there whatsoever. If this whole trigger cube thing isn't even possible the way i'm doing it, then that's my bad. Thanks in advance for any help! Here is the positioning:
Hmmm...it seems that when i tick the answer, a message comes up saying that the question already has an accepted answer, and to remove the accepted status of the other one....but there isn't another answer. Sorry, this is my first time using this system so, even though it's not green for me, is it still answered and therefore cleared? Or is there a bug that isn't letting me untick an answer that isn't here...
Oh that's wacky - yeah - there is no accepted answer here. You should just be able to click the tick under my answer below and it should go green.
You are right - my magic "mark this answer" button I get for having so much $$anonymous$$arma also says that there is already an accepted answer - must be a bug in UA.
is there a way to contact a mod to fix it? i don't want to clutter the forums but it seems to be some sort of error.
Answer by whydoidoit · Sep 19, 2012 at 07:05 AM
The FPS is considering the child boxes to be part of itself because they don't have a rigidbody attached. Attach rigidbodies to the game objects with the colliders and set isKinematic = true.
whydoidoit! That worked! I thought the Character Controller was a physics replacement for rigidbody, not something different. I put a rigidbody on the FPS Controller and now it works! Thank you both very much for you time and quick responses. justinl: just for closure the Debug.Log does indeed show the "Wall Hit", in case you still wanted to know. Thanks again buds!
Hey Brizz104, if $$anonymous$$ike's answer is correct, please indicate his answer as the correct answer by accepting it (top left of the question). Cheers!