- Home /
Raycast is not hitting the first person controller, but works fine with third person character controller
I'm making a game that lets you swap between first and third person, and in it the enemy is made to check line of sight using a raycast (in the same way as the stealth tutorial enemies) to see if there is anything in the way of seeing the player. This all works fine in third person when there is an actual model for the player, but when in first person and just using the first person controller the raycast does not register as hitting it. I've tried looking with the debug drawray but it's not given me much more information. When I've tried looking it up people have usually had the opposite problem!
The playerInSight variable does not get set to true when I'm in first person.
here is the method that includes the raycast:
void OnTriggerStay (Collider other)
{
// If the player has entered the trigger sphere...
if(other.gameObject.CompareTag("Player"))
{
playerDistance = Vector3.Distance(other.transform.position + transform.up, transform.position + transform.up);
// By default the player is not in sight.
playerInSight = false;
// Create a vector from the enemy to the player and store the angle between it and forward.
Vector3 direction = other.transform.position - transform.position;
float angle = Vector3.Angle(direction, transform.forward);
// If the angle between forward and where the player is, is less than half the angle of view...
if(angle < fieldOfViewAngle * 0.5f)
{
RaycastHit hit;
// ... and if a raycast towards the player hits something...
Debug.DrawRay(transform.position + transform.up, direction.normalized*30, Color.red, 0.1f, true);
if(Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius*20))
{
// ... and if the raycast hits the player...
if(hit.collider.gameObject.CompareTag("Player"))
{
// ... the player is in sight.
playerInSight = true;
// Set the last global sighting is the players current position.
lastSighting = other.transform.position;
}
}
}
}
}
because it's a distance not a direction in the debug one
Does this include the code for switching between first and third person? It's hard to tell what's going on without. $$anonymous$$ake sure the first person controller setup has a capsule collider attached to the first person game object?
No it doesn't, but it doesn't really make a difference because the game starts in first person and it doesn't work then, so I don't think the change is breaking it. The first person controller is unity's standard asset FPSController, it comes with a character controller on it and from what I've looked up that -is- a collider, so it should work. The character controller is the only collider on the third person one as well and that works fine.
The only thing I can possibly think is that it doesn't like it because it's a transparent object... but I can't find anything in the documentation that says that should be a problem since it's based on colliders...
Are both the third person model collider and the CharacterController in the same layer? Are both tagged as "Player"?
yup. They are not both active at the same time though
I assume this issue wasn't corrected in the last three years?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Clicking on an Object to Make it the Variable Target 1 Answer
Raycasting on mask doesnt work 2 Answers
Help with "inventory" system c# 1 Answer