- Home /
Trying to figure out if one object can see another
I've got two objects in a scene. A spawn point and a player. I only want the spawn point to be active if the player can't see it.
This is a bit of code I'm trying to use.
Ray ray = new Ray(transform.position, (transform.position - sm.currPlayer.transform.position).normalized);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100.0f))
{
if(hit.collider.gameObject.layer == 10) //If we hit a wall
{
Enemy z = Instantiate(e, transform.position, e.transform.rotation) as Enemy;
}
}
The walls are all on layer 10. So my thinking is that we create a ray from the spawn point to the player. If it collides with one of these walls. That means the player likely can't see the spawn point and go ahead and instantiate an enemy. Otherwise dont. But what is happening is that the enemy is spawning even the player is looking directly at the spawn point. Any help would be much appreciated.
Well your ray is pointing backwards - you should be taking transform.position from the target not vice versa.
Alright I changed that and what happened is that now it only spawns if the point is being looked at. But it definitely helps!
Your answer

Follow this Question
Related Questions
Raycast help: casting 3 rays, out of the faces of a cube 2 Answers
Problem with raycasting when checking neighbouring objects? 0 Answers
What is the best way to navigate through a grid based level using Raycasting ? 2 Answers
How could I combine melee weapons and rays? 1 Answer
Changing position of a RayCast 1 Answer