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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

linecast acting strangely

Discussion in 'Physics' started by AHSVGD5, May 9, 2022.

  1. AHSVGD5

    AHSVGD5

    Joined:
    Apr 27, 2022
    Posts:
    2
    I'm trying to have a player detection script for an enemy in my game. I had it set up so that whenever the player enters a trigger in front of the enemy it calls a linecast to see if the enemy should be able to see the player. This script never worked so I ended up making a test script to just check if I had my linecast set up properly.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Test : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     void FixedUpdate()
    8.     {
    9.         if (Physics.Linecast(transform.position, target.position))
    10.         {
    11.             Debug.Log("blocked");
    12.         }
    13.         else
    14.         {
    15.             Debug.Log("unblocked");
    16.         }
    17.     }
    18. }
    For some reason this only sends back that the linecast is blocked by something regardless of if there is or isnt something in the way. I tested further by making an empty scene so there wouldnt be any colliders messing it up but it still wont work. I have absolutely no clue what could cause this to mess up or what I could do to fix it. Any ideas?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    To be clear it'll hit any collider at all because you don't specify any layer-mask filters. Yes, it obviously shouldn't return anything if there are no colliders in the scene!

    It does sound like you need to debug it more. By this I mean, you can debug it by looking at the RaycastHit which tells you what you actually hit rather than saying it definately shouldn't hit anything here. Have you done that? What does it return if there's no collider in the scene?

    But yes, an empty scene with no colliders and this returning true would be odd in the extreme and a serious problem. If you've got such an empty project with this script that reports it's hit something them I would absolutely not hesitate to report it as a bug!
     
  3. AHSVGD5

    AHSVGD5

    Joined:
    Apr 27, 2022
    Posts:
    2
    Thank you so much! Turns out it was just colliding with another collider I had forgot to disable on the player.