Search Unity

Raycasting issue

Discussion in '2D' started by DigitalDevils, Jul 4, 2015.

  1. DigitalDevils

    DigitalDevils

    Joined:
    Feb 8, 2014
    Posts:
    6
    Please also check the video below.

    I'm trying to make the player, the origin of the raycast.
    When I draw the ray, the origin is the soldier, so it appears to be correct.
    But the 0 point is the lower left corner of the screen instead of the player itself.

    How can I make the player the zero point please ?

    Code (CSharp):
    1. void Update () {
    2.        
    3.         aimFrom.y = player.transform.position.y;
    4.         aimFrom.x = player.transform.position.x;
    5.  
    6.          direction =  (Input.mousePosition - player.transform.position).normalized ;
    7.        
    8.         Physics2D.Raycast (aimFrom,direction);
    9.         Debug.DrawRay(aimFrom,direction);

     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Input.mousePosition is in screen space, whereas player.transform.position is in world space. You need to convert one of the two, usually Input.mousePosition. You can use Camera.ScreenToWorldPoint() to do that.