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

2D raycast???????????????

Discussion in 'Scripting' started by smokegun, Apr 20, 2014.

  1. smokegun

    smokegun

    Joined:
    Dec 13, 2012
    Posts:
    170
    Hello,

    i have a little problem i have 3d and 2d stuff in my game i use:

    Code (csharp):
    1. void OnMouseOver (){
    2.         renderer.material.color = Color.red;
    3.        
    4.         if(Input.GetMouseButtonDown(1)){
    5.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    6.             RaycastHit hit;
    7.            
    8.             if (Physics.Raycast(ray, out hit)){
    9.                 posiX = hit.collider.gameObject.transform.position.x;
    10.                 posiY = hit.collider.gameObject.transform.position.y;
    11.                
    12.                 Instantiate(Tile, new Vector3 (posiX, posiY, 0), Quaternion.identity);
    13.                 Destroy(hit.collider.gameObject);
    14.             }
    15.         }
    16.     }
    To replace my 3d stuff but this script doesn't work with 2d object so my question is how do you do this in 2D?
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  3. smokegun

    smokegun

    Joined:
    Dec 13, 2012
    Posts:
    170
    Thanks a lot -_-
    Sure i can search that but i can't find an good answer how to find the x and y.
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    One of the first results is the official Unity documentation. It shows you how to get the hit information for the 2d case and from there it is almost identical.

    You would have gotten a far better answer from me if I would have had at least the impression that you tried it :)