Cone 'Line of Sight' will not trigger on collision
I can't for the life of me figure this out.. Trying to get an enemy to see the player, or any objects really.. I made a child cone in the enemy to represent line of sight, this code here should trigger whenever that cone intersects with another object, but it just won't.
public void checkSight()
{
if (alive)
{
RaycastHit rayHit;
if (Physics.Linecast(eyes.position,player.transform.position, out rayHit))
{
print("hit "+rayHit.collider.gameObject.name);
}...
Thank you so much for your help.. been pulling my hair out over this.
I haven't used Linecast not sure about your syntax but...your method is checkSight and most are uppercase first letter so just double check you are actually calling checkSight() and not CheckSight() ? And of course that "alive" is true.
Thanks for taking a moment to respond.. yes I've made sure that checkSight() is camelcase, and and set bool alive to true
Have you changed any of the Physics interaction matrix / layermasks / layers these things are on?
@getyour411 I've set the enemy layer to ignore rayCast, I thought the cone itself was in the way or something, but no dice. I just tried messing with the interaction matrix, all the boxes were checked
Your syntax is fine, I tested it in an empty project. While doing so, I noticed this works a little different than I thought. Does Zombear have a character controller or collider on it (that eyes are maybe behind)?
Edit: It might be simplest to change this temporarily to a simple Raycast and query the hit
No, it doesn't have a character controller or colliders, except for the cone trigger. I tried doing Raycast ins$$anonymous$$d of Linecast
public void checkSight()
{
if(alive)
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(eyes.position, fwd, 10))
{
print("hit " + gameObject.name);....
didn't work. I'm pretty new to this so I don't understand enough to convey the problem very well, I really appreciate your time helping me with this, I think I need to move on to another project and try to come back to this when I have a better understanding of the engine and the language though. Seriously, thanks again
Wait -- the code is just a line. What do you mean by "cone" and "trigger"? You can perform a raycast, spherecast, etc... without needing any gameObjects to represent them.
Do you have an actual cone gameObject, set as a trigger, somewhere? If so, there's probably something wrong with that (and the code above is a red herring.)
Yes, what I did was I made a cone object, "eyes" as a child of my enemy object, and set it as a trigger. I have this script for the player, so that when the player enters the cone, it will trigger the checkSight function. Here is the code for player..
public class player : $$anonymous$$onoBehaviour{
public bool alive = true;
void onTriggerEnter(Collider other)
{
if (other.gameObject.name == "eyes")
{
other.transform.parent.GetComponent<monster>().checkSight();
}
}
}
Your answer
Follow this Question
Related Questions
Physics Raycast not working 1 Answer
Raycast hit.point differs from Ray direction [SOLVED] 1 Answer
Adding MaxDistance to RayCast Stops Raycast From working 0 Answers
RayCast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask) question 0 Answers
Issues with detecting collision with objects that have certain tags using Raycast 0 Answers