Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Physics2D.Raycast layers argument not working

Discussion in 'Scripting' started by Punfish, Dec 31, 2014.

  1. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    I have everything in my game on layer 0(default), and my player on User Layer 8.

    I'm sending the raycast from within the player collider and it's hitting the players own collider. If I start the raycast outside the collider it works fine. But this is so much extra work and the collider location could potentially change in perspective to where I need to cast from.

    Here are some things I've tried, and all methods return hit.distance 0.

    Code (csharp):
    1. float distance = 1.5f;
    Code (csharp):
    1.         int castLayer = 1 << 8;
    2.         castLayer = ~castLayer;
    Code (csharp):
    1.         int castLayer = 1 << 8;
    Code (csharp):
    1.         int castLayer = 8;
    Code (csharp):
    1.         int castLayer = 0;
    Code (csharp):
    1. //rayStart is the x/y of the player
    2. hit = Physics2D.Raycast(rayStart, new Vector2(0, -1), distance, castLayer);
    Every single article I read suggest I'm doing this right.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can use an empty child object as a point to start your raycast from.
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    you need layermask to be NOT layer 8, which is
    ~(1<<8)

    Or just turn the option off
     
    Last edited: Dec 31, 2014
    Mycroft likes this.
  4. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    BoredMormon, that's an interesting idea, but wouldn't it still hit the colliders in the parent object?

    hpjohn, the first code example where I'm setting the layermask is using ~(1<<8) and yet the raycast still hits. I could temporarily toggle the raycast start in colliders option but I would think raycast would work properly in the first place.
     
  5. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    Okay, so I figured out the problem. User error of course. I wasn't casting the ray far enough to hit anything so it was always returning a null value. I some reason thought I was casting it far enough. That however brings me to my next question, how do I tell distance in my game between objects? Is the only way to raycast? I don't mean on the fly but as a general idea.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Not if its outside the parent colliders. Think just in front of a gun muzzle, or just below feet.

    Code (CSharp):
    1. Vector3.Distance (transform.position, otherTransform.Position
     
  7. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    327
    Thanks, but I was aware of this already. I guess I was more so wondering if the Unity interface had a way of showing you distance such as a ruler like most photo editing tools. It would make a lot of sense to add such a feature if not.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    To see distances in a scene its common to use a unit cube. You could build a distance ruler functionality relatively quickly.
     
    Punfish likes this.
  9. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I made a measuring tool:
    Code (CSharp):
    1. //Assets/Editor/QuickDistance.cs
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. public class QuickDistance : Editor {
    7.     [MenuItem( "GameObject/Measure Distance Between 2 GOs" )]
    8.     static void MeasureDistance () {
    9.         Debug.Log( ( Selection.transforms[0].position - Selection.transforms[1].position ).magnitude + " Units between " + Selection.transforms[0].name  + " + " + Selection.transforms[1].name );
    10.     }
    11.     [MenuItem( "GameObject/Measure Distance Between 2 GOs", true )]
    12.     static bool ValidateMeasureDistance () {
    13.         return Selection.transforms.Length == 2;
    14.     }
    15. }
    16.  
    Put into Assets/Editor, select 2 GOs in the scene, and point to GameObjects-> Measure Distance

    Ez
     
    Punfish and Kiwasi like this.