- Home /
Detect RaycastHit on Character Controller
As you may know, colliders can't be used with character controllers. I have a script using raycast that is supposed to detect a hit on the player, which has a character controller component attached. How can I get the player to detect a raycast hit?
Answer by Griffo · Nov 25, 2012 at 08:44 PM
if(hit.collider.gameObject.tag == "Player"){
// Do Stuff
}
It doesn't help anymore than my code:
if (hit.collider.name == "Player") { // Code }
Char controllers are really odd beasts. Their built-in capsules count as rigidbody colliders for most purposes. Sort of an undocumented(?) feature.
It's not really an undocumented feature ;) As you can see on the CharacterController page the CharacterController is derived from Collider, so it's just a normal collider.
$$anonymous$$eep in $$anonymous$$d that the CC movement (the $$anonymous$$ove and Simple$$anonymous$$ove functions) bypass the physics and perform their own physics when moved. Besides that it's just a capsule collider (which can only be rotated around the y-axis btw).
It doesn't make much sense to use GameObject.Find when you already have a reference to your player. Especially when it might be a multiplayer game there might be multiple objects named player.
Just do it like this:
if (Physics.Raycast(transform.position, forward, hit, range))
{
if (hit.collider.name == "Player")
hit.collider.GetComponent(Health).decreaseHealth(25);
}
Do you get any errors in the console?
If so it's most likely that the object you've hit, that is named "Player" doesn't have a Health script attached.
If you don't get an error you doing something wrong.
I've just took a look at your prefab package. It's a bit messed up on my end. Your scripts are not included and the child objects have no name. Furthermore you still have an additional capsule collider as child which might intercept your raycast. Remove this collider. Again, the CharacterController IS already a collider.
Answer by Owen-Reynolds · Nov 26, 2012 at 02:49 PM
There are a lot of things that can go wrong with ray casts: bad direction, too short, hitting a trigger box by mistake... so best to do lots of checking. Try to check each step. This would check where it ends and what you hit:
var rayEnd : Transform // drag in a small red cube w/no collider
var hitWho : string; // debugging
// Update:
// so we can see where ray goes:
rayEnd.position = transform.position + forward*range;
if(Physics.Raycast( ... ) {
hitWho="hit:"+hit.transform.name;
...
}
else hitWho="<no hit>";
Some people also recommend using `Debug.DrawRay` to see the path, but the inputs aren't the same as raycast, so it's easy to draw it in the wrong place and confuse yourself even more.
I don't understand the purpose of doing that. It doesn't seem to work.
@Coreyf716: You don't understand the purpose of debugging? A computer never does something the wrong way. It always just do what you or another programmer told it to do. It seems you don't get the result you would have expected, so you have a bug in your game / program.
It's very unlikely that there is a bug in Unity itself, it's possible but you shouldn't start with that assumption.
Since it's not doing what you want it's your task to figure out what it is actually doing, either to understand how it works and / or to spot the problem. By storing the name of the object your ray hits in a public variable you can see in the inspector what your raycast has hit.
Yeah, I understand it. It's the way that my code and his worked together.