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

Raycast in ExecuteInEditor (OnSceneGUI) problems

Discussion in 'Editor & General Support' started by Yearl, Oct 2, 2015.

  1. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    Hi everybody !

    I'm working my own painting tiled sprites tool, and encounter some problems ...
    I have a grid of GameObjects with Sprite component and 2DBoxCollider.
    When I click on which of one in editor scene (NOT IN RUNTIME), I wanna be able to get his Sprite properties.

    Then, I arlready setup my tool script and disable standart object selection in Sceneview:

    Code (CSharp):
    1. HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive));
    And I have my current event checking if click working great ... But when I try Raycast, hit nothing.

    Code (CSharp):
    1. Ray ray = Camera.current.ScreenPointToRay(current.mousePosition);
    2.                 RaycastHit hit = new RaycastHit();
    3.                 if(Physics.Raycast(ray, out hit, Mathf.Infinity, 10))
    4.                 {
    5.                     Debug.Log("Drawing");
    6.                 }
    For moment, Raycast only test if hiting.
    Raycasts have to hit only Sprites in Grid ... I'm not good with Raycasts and don't see what I missed :/

    Hope can help me !

    EDIT: All my sprites are in a layer which don't ignore raycast, and the "Raycast Hit Trigger" option is activate.
     
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,672
  3. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    I can't that was a thing like this.
    I spent so much time to find another solution !

    Fixed right now, thanks a lot !

    EDIT: facing to another problem.
    My new code :

    Code (CSharp):
    1. Ray ray = Camera.current.ScreenPointToRay(current.mousePosition);
    2.                 ray.origin = new Vector3(ray.origin.x, ray.origin.y*-1, -100f);
    3.                 RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
    4.                 if(hit.collider != null)
    5.                 {
    6.                     Debug.Log("Drawing");
    7.                     //set the selected sprite
    8.                     hit.collider.gameObject.GetComponent<WhichSpriteIn>().whichSprite = allMySprites.spriteSelected;
    9.                     //syncsprite
    10.                     hit.collider.gameObject.GetComponent<WhichSpriteIn>().syncSprite();
    11.                 }
    First I must invert the Yaxis to work great and don't know why.
    But my problem is that when my camera(sceneview camera, not main for play) isn't centered at center of the world, the raycast is offsetted on Yaxis.

    Exemple:
    Camera centered/ When I click on my sprite It's ok, select the good one under my mouse.
    Camera uncentered/ When I click, select the sprite below or above my mouse and more the camera is uncentered, more the offset is big.

    Hope i've been clear .. Any recommandations ?
     
    Last edited: Oct 2, 2015
  4. Peez-Machine

    Peez-Machine

    Joined:
    Jul 30, 2013
    Posts:
    27
    Instead of Physics2D.Raycast, try Physics2D.GetRayIntersection. Raycast is meant for detecting collision in the X-Y plane. If you have a 2D platformer and are raycasting within your world, that's what you want. GetRayIntersection, however, projects a 3D vector and see if pierces a 2D collider. I'm guessing that what you're trying to do is cast a ray from your camera toward the mouse cursor and see which sprite the ray ends up hitting in your world. In that case, you definitely want GetRayIntersection.
     
  5. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    Ok, I just tried with GetRayIntersection, but the "offset problem" still the same ...
    Am I wrong with setting the Ray ?