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. Dismiss Notice

Get XY Pos of Mouse?

Discussion in 'Scripting' started by Faestus, Nov 11, 2014.

  1. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    So, I tried getting the XY pos of the mouse on screen like this:
    Code (CSharp):
    1. RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    2.        
    3.         if(hit.collider != null)
    4.         {
    5.             Debug.Log ("Target Position: " + hit.collider.gameObject.transform.position);
    6.             PosXY=new float[]{hit.collider.gameObject.transform.position.x,hit.collider.gameObject.transform.position.y};;
    7.         }
    However, bith X and Y are 0, and it doesn't log anything. Anybody know why?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You are casting the ray along direction (0,0) which doesnt make sense
     
  3. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    So Vector2.one?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I don't know!
    Which direction do you want to cast a ray?
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457