Search Unity

Drawing on screen/drawing off-center from mouse

Discussion in '2D' started by FolleseGaskarth, Feb 20, 2019.

  1. FolleseGaskarth

    FolleseGaskarth

    Joined:
    Oct 2, 2018
    Posts:
    1
    Hey everyone,

    So I'm kind of new at Unity but I've been working with it for a few months now. I've tested a few different implementations but I can't seem to find any sort of way to allow the user to draw on the screen. Originally I had a trail prefab and made multiple instances of that but I couldn't find a way to allow the user to erase or even clear the screen.

    Then I tried the following implementation which is a similar concept but I'm instantiating a sprite prefab of a dot to give the illusion of drawing a line.

    Code (CSharp):
    1. public Transform baseDot;
    2.     public KeyCode mouseLeft;
    3.     public static string toolType;
    4.  
    5.     void Update()
    6.     {
    7.         if (Input.GetKey(mouseLeft))
    8.         {
    9.  
    10.             Vector2 mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    11.             /*Vector2 objPosition = Camera.main.ScreenToWorldPoint(mousePos);*/
    12.  
    13.             Instantiate(baseDot, mousePos , baseDot.rotation);
    14.         }
    15.     }
    I purposely left out the declaration of objPosition because it wouldn't work (draw on the screen) if I instantiated with the objPosition's location, which I also don't understand.

    My issue here is that for some reason the dots draw off center from the mouse click and go more off center depending on where I draw. If I keep the mouse towards the center of my screen, they are on center with my mouse. However the further I keep moving and drawing from the center of the screen, the more the dots keep getting off center with my mouse, as if they are being pulled towards the center. Also in the actual build, the dots are even more off center.

    This is what I'm having trouble with with this implementation and am hoping to fix it since I know I can get the user to erase this way. I thought the problem may have been with my camera since this is a 2D game and my camera is in perspective so I put it in orthographic but then my instantiations stop being visible (the editor shows them coming up in the hierarchy panel but it does not show in game window).

    So essentially my issue is that I need to find a way to either:
    - Erase or clear ALL instances of a trail at the click of a button
    OR
    - Get the instantiation of my dot sprite prefab to stop being off center.

    Any sort of advice would be really helpful as I'm just thinking of switching everything around into a different method if I don't get any headway on this.
     
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311