- Home /
Missile colliding with Character Controller
Hello all,
I'm creating a missile via script attached to the first person controller. The only problem is that my missile collides with the character controller's collider before it moves anywhere. I've tried to use Physics.ignoreCollision() but it doesn't seem to work with the character controller.
function FireRocket () {
var rocketClone : Rigidbody Instantiate(rocket, transform.position + Vector3 (0, 3, 0), transform.rotation);
Physics.IgnoreCollision(rocketClone.collider, transform.root.GetComponent(CharacterController).collider);
...
I've used some printouts and determined that the missile is indeed colliding with the "CharacterController"
Physics.IgnoreCollision(rocketClone.collider, transform.root.collider);
doesn't work either.
Any ideas? I could use layers, but I'd rather do it without them.
EDIT: I've done more testing, and my IgnoreCollision call points to the exact same object/collider that is colliding with my missile. I really don't understand why this isn't working.
Physics.IgnoreCollision(rocketClone.collider, transform.GetComponent(CharacterController).collider);
^^is the call that I'm using now, just to avoid any confusion. (they all should point to the same collider anyway)
Cheers,
Unislash
Answer by Unislash · Jan 16, 2011 at 05:47 PM
This turned out to be a very tricky problem. It turns out that my missile is a prefab, and has a core child for creating particles. The core for some reason had a box collider on it.
It appears that unity interprets any child collider as a collision with the parent too, so that was setting off the collision with the character controller--even though I turned the parent collider's collision with the character controller off.
I hope this helps someone, because it took me >4 hours to find this one!
Cheers,
Unislash
Answer by fireDude67 · Jan 15, 2011 at 06:49 PM
Instantiate()
your missile farther away?
Well clearly. But I don't want to have to deal with moving my missile generation based on who the player is.
Answer by PrimeDerektive · Jan 15, 2011 at 07:02 PM
transform.root.collider would only work if this rocket was a child of the player object, and thats not something you want to do (the rocket would move around when the player moved around).
You'll have to cache a reference to the player in this script somehow, probably by making a
public var player : GameObject;
And dragging your player object from the scene to this exposed var in the inspector. Then use IgnoreCollision on the player's collider from there:
Physics.IgnoreCollision(rocketClone.collider, player.collider);
Wouldn't transform.root.collider point to the root of the transform that the script is attached to? In this case, that is the player. In any case, this still doesn't work. See edit for updated information :-)
Aw crap i'm sorry; misunderstood. I was thinking the ignore code was going in a script on the instantiated projectile for some reason.