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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Linecast acting weird, buggy??

Discussion in 'Scripting' started by symorian, Jun 8, 2017.

  1. symorian

    symorian

    Joined:
    Jun 7, 2017
    Posts:
    69
    Hi,

    I have an enemy LOS script where it detects when the player is collided with it.
    At the very beginning stages I used Box collider with that LOS script and then decided to switch to linecast for the sake of decreasing amount of colliders and visually having a better scene.

    BUT,

    when I was working with collider type of thing, it was quite precise.
    As I swtiched to linecast, I see that I can abuse the enemy LOS. I mean, when I just stand at the very edge of enemy LOS, the enemy acts like he sees me but the projectiles are got reset when he fires as if he is undecided rather he sees me or not and I don'T get any hit despite his linecast collides with my collider.

    But when I just move forward a little bit, he fires the projectiles properly and I get hits.

    What is it causing this strange behavior with linecast?? o_O

    Here is my code...

    Code (CSharp):
    1.  
    2.  
    3. private void Update()
    4.     {
    5.         Seeking();
    6.     }
    7.  
    8.     void Seeking()
    9.     {
    10.  
    11.         Debug.DrawLine(losStart.position, losEnd.position, Color.red);
    12.         playerLocated = Physics2D.Linecast(losStart.position, losEnd.position, 1 << LayerMask.NameToLayer("Player"));
    13.         if (playerLocated && !player.Instance.IsDead)
    14.         {
    15.             enemy.Target = player.Instance.gameObject;
    16.  
    17.         }
    18.         else
    19.         {
    20.             enemy.Target = null;
    21.         }
    22.  
    23.     }
    24.  

    Thank you all!
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    I'd like to see the script for what happens after the player is made the enemy's target.
     
  3. symorian

    symorian

    Joined:
    Jun 7, 2017
    Posts:
    69
    I found the problem, though I haven't yet inspected it to the full depth.
    I have written a projectile range for my enemy. If the range of the projectile is less than the enemy LOS, this weird behavior starts. But if the range of the projectile is equal or greater than the LOS, everything seems to work normal.

    I'll dig it further to see what causes this strange behavior. =)
    Sometimes I get lost in my own code... :p
     
  4. jhon-avila

    jhon-avila

    Joined:
    Sep 20, 2014
    Posts:
    2
    Did you actually found out what is the issue ?