Search Unity

No RayCast Result

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Jan 3, 2016.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,295
    Admittedly, not a huge thing if I can't get it working, but makes debugging much easier.

    I am trying to change the color of my Debug.DrawRay call so I can visually see if the raycast is hitting, but I am afraid that either my ray is not detecting the hit, or there is something else wrong.

    A pretty straightforward start to an AI for out game.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MainAiScript : MonoBehaviour
    5. {
    6.     private GameObject player;
    7.     private float layer = 0f;
    8.  
    9.     void Start()
    10.     {
    11.         player = GameObject.FindGameObjectWithTag("Player");
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         RaycastHit hit;
    17.         float distanceToTarget = Vector3.Distance(player.transform.position, transform.position);
    18.         Vector3 direction = (player.transform.position - transform.position).normalized;
    19.         Ray ray = new Ray(transform.position, direction * distanceToTarget);
    20.  
    21.         Color rayColor = Color.red;
    22.  
    23.         Debug.DrawRay(transform.position, direction * distanceToTarget, Color.red);
    24.  
    25.         if (Physics.Raycast(ray, out hit, layer))
    26.         {
    27.             if(hit.collider.tag == "Player")
    28.             {
    29.                 rayColor = Color.green;
    30.             }
    31.             else
    32.             {
    33.                 rayColor = Color.red;
    34.             }
    35.         }
    36.     }
    37. }
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    You are changing the color after the ray was drawn.
     
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,295
    I tested the raycast, it never hits either.
    I am following a segment I have used before so I don't know why it isn't returning anything.
     
  4. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    Hey, please check the Layer tag of the gameobject you are hitting. If it has "Ignore raycast" then change it.
     
  5. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The layer is a float in your code, which makes absolutely no sense! Check the scripting reference for the correct usage of the method. Also keep in mind that it is supposed to be a layer mask and not a layer.
     
  6. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,295
    The layer is simple the default layer, I tend not to use layers much, I use tags more.
     
  7. Kamil-Says

    Kamil-Says

    Joined:
    Jun 30, 2014
    Posts:
    154
    So, did you try to check with debug log?
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    To only thing that needs to be checked is the API and how it is supposed to be used. At the moment the "layer" is actually the distance or length of the raycast.
     
  9. Ysgramor

    Ysgramor

    Joined:
    Jan 23, 2014
    Posts:
    69
    Code (CSharp):
    1.  Debug.DrawRay(transform.position, direction * distanceToTarget, Color.red);
    this make the ray always red right?