Search Unity

Raycasting - all direction issue

Discussion in 'Scripting' started by Homicide, Feb 14, 2018.

  1. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    So i have googled this, and it appears many people have experienced this, yet no thread i read has solved it for me.

    im trying to cast a ray in north, east, south , west directions, and im also using Debug.DrawRay, which shows all raycasts directions are fine, and each of the raycasts indeed detects an object... until i have them all turned on.

    then only east, and south are detecting, while north and west do nothing. it seems if i turn off north, or any direction really, other directions respond. im confused as to why this is happening.

    thanks in advance, as i realise this isnt a new issue, and probably not even a hard one.;

    here the code.
    Code (CSharp):
    1. public class CellCaster : MonoBehaviour {
    2.  
    3.     public float castDistance = 0.0F;
    4.     public bool castNorth;
    5.     public bool castEast;
    6.     public bool castSouth;
    7.     public bool castWest;
    8.  
    9.     Vector3 north;
    10.     Vector3 east;
    11.     Vector3 south;
    12.     Vector3 west;
    13.  
    14.     private Ray northCasterRay;
    15.     private Ray eastCasterRay;
    16.     private Ray southCasterRay;
    17.     private Ray westCasterRay;
    18.  
    19.     void VerifyAdjacentCells(){
    20.        
    21.         RaycastHit hit;
    22.  
    23.         if (castNorth) {
    24.             northCasterRay = new Ray (transform.position, north);
    25.             if (Physics.Raycast (northCasterRay, out hit, castDistance)) {
    26.                 Debug.Log ("Northern Contact made with " + hit.collider.gameObject.name);
    27.             } else {
    28.                 Debug.Log ("No Contact");
    29.             }
    30.         }
    31.         if (castEast) {
    32.             eastCasterRay = new Ray (transform.position, east);
    33.             if (Physics.Raycast (eastCasterRay, out hit, castDistance)) {
    34.                 Debug.Log ("Eastern Contact made with " + hit.collider.gameObject.name);
    35.             } else {
    36.                 Debug.Log ("No Contact");
    37.             }
    38.         }
    39.         if (castSouth) {
    40.             southCasterRay = new Ray (transform.position, south);
    41.             if (Physics.Raycast (southCasterRay, out hit, castDistance)) {
    42.                 Debug.Log ("Southern Contact made with " + hit.collider.gameObject.name);
    43.             } else {
    44.                 Debug.Log ("No Contact");
    45.             }
    46.         }
    47.         if (castWest) {
    48.             westCasterRay = new Ray (transform.position, west);
    49.             if (Physics.Raycast (westCasterRay, out hit, castDistance)) {
    50.                 Debug.Log ("Western Contact made with " + hit.collider.gameObject.name);
    51.             } else {
    52.                 Debug.Log ("No Contact");
    53.             }
    54.         }
    55.     }
    56.    
    57.     // Update is called once per frame
    58.     void Update () {
    59.         north = transform.TransformDirection (Vector3.back * castDistance);
    60.         east = transform.TransformDirection (Vector3.left) * castDistance;
    61.         south = transform.TransformDirection (Vector3.forward) * castDistance;
    62.         west = transform.TransformDirection (Vector3.right) * castDistance;
    63.         if(castNorth)
    64.             Debug.DrawRay(transform.position, north, Color.green);
    65.         if(castEast)
    66.             Debug.DrawRay(transform.position, east, Color.green);
    67.         if(castSouth)
    68.             Debug.DrawRay(transform.position, south, Color.green);
    69.         if(castWest)
    70.             Debug.DrawRay(transform.position, west, Color.green);
    71.         VerifyAdjacentCells();
    72.     }
    73.  
    74.     void OnDrawGizmos(){
    75.         Gizmos.DrawIcon (transform.position, "cc.png");
    76.     }
    77. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Just a note, in update your north is different then your other three. Now, it may behave the same, but something to be aware of how you have your ( ) as this can matter for some things.
     
  3. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    ahh yea good eye. my bad, luckily that didnt affect anything and create some bad brew.

    ive corrected that now, but still the problem remains. if i have all 4 directions turned on, it only detects south and west. north and east do nothing. :(

    it really confuses me because if i toggle only one of any direction, they all work. just not turned on together.

    ty for that.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Just curious, but have you tried replacing the out hit and have each one use it's own variable instead? I'm not sure that will matter or not honestly, but at a glance I'm not seeing anything wrong with the code itself.
     
  5. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    i just thought of that and yes, did try it. each with its own. Sigh. I did think it was all correct but yet, something not working.

    Again, its odd that when toggled on alone, all north, east, south, west do work and detect an object. But as soon as i start toggling more then one... :( haha

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CellCaster : MonoBehaviour {
    6.  
    7.     public float castDistance = 0.0F;
    8.     public bool castNorth;
    9.     public bool castEast;
    10.     public bool castSouth;
    11.     public bool castWest;
    12.  
    13.     Vector3 north;
    14.     Vector3 east;
    15.     Vector3 south;
    16.     Vector3 west;
    17.  
    18.     RaycastHit hitNorth;
    19.     RaycastHit hitEast;
    20.     RaycastHit hitSouth;
    21.     RaycastHit hitWest;
    22.  
    23.     private Ray northCasterRay;
    24.     private Ray eastCasterRay;
    25.     private Ray southCasterRay;
    26.     private Ray westCasterRay;
    27.  
    28.     void VerifyAdjacentCells(){
    29.  
    30.         if (castNorth) {
    31.             northCasterRay = new Ray (transform.position, north);
    32.             if (Physics.Raycast (northCasterRay, out hitNorth, castDistance)) {
    33.                 Debug.Log ("Northern Contact made with " + hitNorth.collider.gameObject.name);
    34.             } else {
    35.                 Debug.Log ("No Contact");
    36.             }
    37.         }
    38.         if (castEast) {
    39.             eastCasterRay = new Ray (transform.position, east);
    40.             if (Physics.Raycast (eastCasterRay, out hitEast, castDistance)) {
    41.                 Debug.Log ("Eastern Contact made with " + hitEast.collider.gameObject.name);
    42.             } else {
    43.                 Debug.Log ("No Contact");
    44.             }
    45.         }
    46.         if (castSouth) {
    47.             southCasterRay = new Ray (transform.position, south);
    48.             if (Physics.Raycast (southCasterRay, out hitSouth, castDistance)) {
    49.                 Debug.Log ("Southern Contact made with " + hitSouth.collider.gameObject.name);
    50.             } else {
    51.                 Debug.Log ("No Contact");
    52.             }
    53.         }
    54.         if (castWest) {
    55.             westCasterRay = new Ray (transform.position, west);
    56.             if (Physics.Raycast (westCasterRay, out hitWest, castDistance)) {
    57.                 Debug.Log ("Western Contact made with " + hitWest.collider.gameObject.name);
    58.             } else {
    59.                 Debug.Log ("No Contact");
    60.             }
    61.         }
    62.     }
    63.    
    64.     // Update is called once per frame
    65.     void Update () {
    66.         north = transform.TransformDirection (Vector3.back) * castDistance;
    67.         east = transform.TransformDirection (Vector3.left) * castDistance;
    68.         south = transform.TransformDirection (Vector3.forward) * castDistance;
    69.         west = transform.TransformDirection (Vector3.right) * castDistance;
    70.         if(castNorth)
    71.             Debug.DrawRay(transform.position, north, Color.green);
    72.         if(castEast)
    73.             Debug.DrawRay(transform.position, east, Color.green);
    74.         if(castSouth)
    75.             Debug.DrawRay(transform.position, south, Color.green);
    76.         if(castWest)
    77.             Debug.DrawRay(transform.position, west, Color.green);
    78.         VerifyAdjacentCells();
    79.     }
    80.  
    81.     void OnDrawGizmos(){
    82.         Gizmos.DrawIcon (transform.position, "cc.png");
    83.     }
    84. }
    85.  
     
  6. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    lol, dude, its fixed somehow... but i didnt do it. hmm.

    im gonna chalk this up as a "Unity hasnt had enough coffee, and its too early in the morning..." kinda thing. :p
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    haha. Sounds good, glad it's working now at least. :)