Search Unity

Raycast from child?

Discussion in 'Scripting' started by RichardsonKevin, Oct 25, 2014.

  1. RichardsonKevin

    RichardsonKevin

    Joined:
    Feb 3, 2013
    Posts:
    43
    Hello everyone!

    I am having a problem shooting a raycast from a child gameobject;

    I have an empty gameobject in front of the AI's eyes to represent a cone of vision.

    I set it up properly in the awake

    Code (CSharp):
    1. private GameObject coneOfVision;
    2.  
    3. void Awake()
    4.     {
    5.         coneOfVision = transform.Find("ConeOfVision").gameObject;
    6.     }
    7.  
    8.  
    a call it in a later function like this

    Code (CSharp):
    1. Vector3 direction = enemy.transform.position - coneOfVision.transform.position;
    2.         float angle = Vector3.Angle(direction, transform.forward);
    3.        
    4.         if(angle < fieldOfViewAngle * 0.5f)
    5.         {
    6.             Debug.Log ("Correct Angle!!!!!!!!!!");
    7.             RaycastHit hit;
    8.            
    9.             if(Physics.Raycast(coneOfVision.transform.position, direction.normalized, out hit, Mathf.Infinity))
    10.             {
    11.                 Debug.Log ("raycast shot");
    12.             }
    13.         }
    14.  
    I get a debug of "raycast shot" when I just use transform.position but I do not get it when I try to use the coneOfVision.transform.position...
     
  2. Deleted User

    Deleted User

    Guest

    Maybe use debug to draw the line and look to see where it's going.
    Also make sure your not ignoring raycast on layers.