Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycast collision problem after changing transform.localScale [solved]

Discussion in 'Scripting' started by Kequc, Nov 12, 2016.

  1. Kequc

    Kequc

    Joined:
    Oct 23, 2015
    Posts:
    21
    I have laid a Quad over top of my scene which I'm using as a grid, and I want to find out what I'm pointing at. The problem seems to be that if I resize the Quad, the Raycast doesn't hit it anymore. If I leave the size alone everything works fine.

    Code (CSharp):
    1.     public void LinkMap (Map map)
    2.     {
    3.         this.map = map;
    4.         // This line is the problem
    5.         Grid.transform.localScale = new Vector3 (map.width, map.height, 0);
    6.         Grid.GetComponent<Renderer> ().material.mainTextureScale = new Vector2 (map.width, map.height);
    7.         Grid.gameObject.SetActive (true);
    8.     }
    9.  
    Code (CSharp):
    1.     public void PerformRaycast ()
    2.     {
    3.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    4.         RaycastHit hitInfo;
    5.        
    6.         if (Physics.Raycast (ray, out hitInfo)) {
    7.             // This always returns stuff under my grid
    8.             Debug.Log (hitInfo.transform.tag);
    9.             if (hitInfo.transform.CompareTag ("MapOverlayGrid")) {
    10.                 // This is the tag assigned to my Grid
    11.                 // Always works if I don't resize the Grid in LinkMap
    12.                 Debug.Log(hitInfo.point.ToString ());
    13.             }
    14.         }
    15.     }
    16.  
    In my editor I can clearly see that resizing works as expected and the object is as I expect it to be. It's above the scene. Is there a known workaround or is there something I should know that I'm not doing after resizing?
     
  2. Kequc

    Kequc

    Joined:
    Oct 23, 2015
    Posts:
    21
    Update:

    Ohhhhhhh that was causing me endless torment.

    Solution it needs `1f` rather than `0`

    Code (CSharp):
    1. Grid.transform.localScale = new Vector3 (map.width, map.height, 1f);
     
    PaSirda and garyredfox like this.