- Home /
The question is answered, right answer was accepted
Raycast Doesn't Hit Character Controller
Hi,
I have an enemy with line and angle of sight. However when I check if it can directly see the player, Raycast never hits the player. To check if it works, I put some other game objects like cubes, and Raycast reports them.
Example:
As you can see, Debug.DrawRay goes through player but RayCast info only gives Plane as the info.
This is the code I am using.
Vector3 direction = other.transform.position - transform.position;
float angle = Vector3.Angle( direction, transform.forward);
if( angle < fieldOfViewAngle * 0.5f){
RaycastHit hit;
if( Physics.Raycast( transform.position + transform.up, direction.normalized, out hit, col.radius)){
Debug.Log("Name: " + hit.collider.gameObject.name + " Tag: " + hit.collider.gameObject.tag);
//if( hit.collider.gameObject == player){
if( hit.collider.gameObject.tag == "Player"){
playerInSight = true;
Debug.Log("Enemy sees Player");
lastPlayerSighting.position = player.transform.position;
//personalLastSighting = player.transform.position;
}
}
}
So you're trying to cast a distance of 15. Just set distance to $$anonymous$$athf.Infinity, why do you want o use the colliders radius?
did you put the player under the "Ignore Raycast" layer?
sounds silly but, did you forget to tag as "Player" in inspector
make sure your tagging the object with the collider and not a child or parent of it that has no collider.
Answer by Tanshaydar · Oct 21, 2013 at 11:26 PM
For anyone out there wondering if there is a solution... Raycasting never detects the Character Controller. It is a special type of collider and it doesn't play well with other components.
I solved my issue through replacing my FPS Controller with Rigidbody FPSWalker. You can find it in here: http://wiki.unity3d.com/index.php?title=RigidbodyFPSWalker
My code and game works now. Thanks everyone who tried to help.
your question solved my problem :)
no raycast doen't hit anything else than normal collider
Raycast doesn't hit even collider trigger means I've got bigger problems than before but, ... now you know what doesn't work.
yes, you must have a collider. I imagined they had one since they were trying to return hit.collider info. Never assume I suppose...
would using Collider.Raycast work? ins$$anonymous$$d of Physics.Raycast
Casts a Ray that ignores all Colliders except this one. So, it is looking for a collider again. Not gonna work on character controller either, but you are welcome to try.
what about adding another collider(on an empty gameobject) as a child to the fps controller?
Follow this Question
Related Questions
Gun script using Physics.Raycast not working? 2 Answers
Continuously rotate a gameobject on 1 axis perpendicularly to another gameobject's normal? 1 Answer
Creating custom character controller collision checking 0 Answers
How to add platform motion to player controller without parenting? - Mostly working code 1 Answer
Infamous first light run system, problem with my code, HELP 0 Answers