Search Unity

raycasting function hit.collider conditional problem [solved]

Discussion in 'Scripting' started by gibberingmouther, Jul 16, 2017.

  1. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    here's my function:

    Code (csharp):
    1.  
    2. void CastRay()
    3.         {
    4.             var heading2 = Input.mousePosition - transform.position;
    5.             var distance2 = heading2.magnitude;
    6.             var direction2 = heading2 / distance2;
    7.             RaycastHit2D hit = Physics2D.Raycast(transform.position, direction2, 10);
    8.             if (hit.collider != null && hit.collider.tag == "Enemy")
    9.             {
    10.                 //Debug.Log(hit.collider.gameObject.enemy);
    11.                 enemy_over = true;
    12.             }
    13.         }
    14.  
    it's supposed to trigger enemy_over if the mouse is over the enemy object and the hero is within range but it doesn't do this. alternatively i heard about something called "mouse picking" where you cast a ray from the camera in line with the mouse position, and return info about what you hit. this is supposedly more desirable but i think i'd still need to calculate the distance between the hero and the enemy.

    anyway, i'll do without "mouse picking" for now - can anybody help me with my raycasting issue?

    edit: i tried adding Camera.main.ScreenToWorldPoint() around Input.mousePosition but still not fixing the issue

    edit2: i also tried:
    Code (csharp):
    1.  
    2. RaycastHit2D hit = Physics2D.Raycast(transform.position, direction2);
    3.             if (Physics2D.Raycast(transform.position, direction2, 100))
    4.             {
    5.                 if (hit.collider != null && hit.collider.tag == "Enemy")
    6.                 {
    7.                     Debug.Log("enemy_over");
    8.                     enemy_over = true;
    9.                 }
    10.             }
    11.  
    can't get the debug.log message to show.
     
    Last edited: Jul 16, 2017
  2. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    it's been a couple days, so bump. usually i try to bump with more info about the problem or any progress i've made, but i checked out a bunch of raycasting related posts and looked at the docs, still no progress on this issue.

    edit: i watched the tutorial now and i think i may have been missing that the hit variable has to be of raycasthit type. we'll see ... should have watched that BEFORE bumping lol

    edit2: now i have -
    Code (csharp):
    1.  
    2. RaycastHit2D hit;
    3.             hit = Physics2D.Raycast(transform.position, direction2, 1000);
    4.             if (Physics2D.Raycast(transform.position, direction2, 1000))
    5.             {
    6.                 if (hit.collider != null && hit.collider.tag == "Enemy")
    7.                 {
    8.                     Debug.Log("enemy_over");
    9.                     enemy_over = true;
    10.                 }
    11.             }
    12.  
    still isn't printing the debug info.
     
    Last edited: Jul 21, 2017
  3. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    You actually shouldn't need the first if statement.. duplicating your raycast. Maybe you should use debug drawray and get a visual of your ray. Often when you have issues the ray isn't how you expected.
     
    gibberingmouther likes this.
  4. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    I couldn't get drawray to work, which now i'm thinking could be showing part of the problem. Maybe direction2 is buggy? I think you're right about the if statement, though predictably fixing that didn't get the debug statement to show.

    Still stuck though i'll take a second look at direction2. It looks like it is the same as the direction code i used in another script, which DID work, so unless i made a mistake somewhere this is a little bit odd.
     
  5. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    could the problem be that i'm using 3d vectors with 2d raycasting functions? i'm short on ideas right now.

    edit: i got debugdrawray to work. i had implemented the function incorrectly. still no progress on getting the debug log to show.

    edit2: since i got debugdrawray to work, i'm certain there is nothing wrong with my direction2 code. i tried 3d raycasting, still no luck. is there something simple i'm missing? i'm still searching around to see if i find something but no luck yet.

    edit3: okay, it's working! it only shows the debug logs if you click the dialogue box button on the console. now i'm getting a different error though - i'll start another thread if i can't figure it out soon. The error is: "You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all." Any input on this?
     
    Last edited: Jul 23, 2017