Search Unity

C# making an sprite to sense touch from mobile device

Discussion in 'Scripting' started by agostonr, Jul 26, 2016.

  1. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Dear All,
    I'm sort of stuck on the following:
    I created some cards. When the player touches them, I want them to turn and show the other side (midway changing the sprite).
    I can't make the script needed to sense the touch. How to do that?
    Thanks in advance.
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    agostonr likes this.
  3. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Thanks for your help. So it works like this: I have those object in the scene, no overlapping, and if I tap the screen on one of them, it just gets activated?
     
  4. TDArlt

    TDArlt

    Joined:
    May 22, 2013
    Posts:
    23
    This is the method you might want to use for that (if you are using the cards on a 2D canvas):
    https://docs.unity3d.com/ScriptReference/RectTransformUtility.RectangleContainsScreenPoint.html

    You just hand over the touch- or mouse coordinates and check it against the RectTransform of your card
    Code (csharp):
    1. if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Ended)
    2. {
    3.   bool clicked = RectTransformUtility.RectangleContainsScreenPoint(theCard, Input.touches[0].position);
    4. } else if (Input.GetMouseButtonUp(0))
    5. {
    6.   bool clicked = RectTransformUtility.RectangleContainsScreenPoint(theCard, Input.mousePosition);
    7. }

    --
    If you have a 3D-object (usual Transform instead of RectTransform), you'll need a collider on that GameObject one and shoot a ray:
    http://answers.unity3d.com/questions/34795/how-to-perform-a-mouse-click-on-game-object.html
    As you mentioned a sprite, I think you have a RectTransform here
     
    agostonr likes this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Also worth pointing out the EventSystem and the PhysicsRaycaster.

    No need to implement your own raycasting for input when it's all built into the engine. As an added bonus this is completely compatible with the GraphicsRaycaster from the UI system.
     
    NeatWolf and agostonr like this.
  6. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Yes, rect transform. The card has only a sprite renderer component on it. I have a 2D canvas, the cards are generated.
    By the way what will happen if the player taps the screen between 2 cards, so both of them return true from that RectangleContains stuff?
    And is this the case you were pointing at, right? (my setup)
     
  7. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    The object need to have a collider of some sort (2D or 3D) or be a GUI object. When there is a click event focused on the object its OnMouseDown() method will run. So, you will not need to add anything extra apart from a collider.

    It is probably the easiest way to do what you are trying to accomplish.
     
    agostonr likes this.
  8. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Fine, I will look into that.
    However, I will have around 50 or so of this object in the scene. won't this collider solution be a burden for mobile devices, if I have that 50?
     
  9. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    No, 50 objects isn't even close to being a burden.
     
    agostonr likes this.
  10. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Thanks for the help. I will start its implementation as soon as I'm finished with my current task.
     
  11. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Thought I will let you and all who will take a look at this thread know, I finished with the screen layout and did this too. I used 3D box colliders with the thread CF-Studios mentioned. (2D box won't work, needs Physics 2D). I have a collider on each of my cards, upon click they do what I told them to do. Thank you all again for the help.
     
  12. TantzyGames

    TantzyGames

    Joined:
    Jul 27, 2016
    Posts:
    51
    You could make each of the cards buttons instead of simple sprites, then it's all taken care of for you. As each one flips, at the halfway point deactivate one side and activate the other. I did this for cards flipping in a project, and was simple and quick.
     
    agostonr likes this.
  13. agostonr

    agostonr

    Joined:
    Jan 3, 2016
    Posts:
    193
    Yes, I thought of buttons, but was unsure if they could be used only on canvases or what. And now this part is finished and working so won't mess with it :D
    You did the turning by animations with an event in the middle right? I wanted the same but I can't access the SwapSprites fuction from the event (basically can't access any event). How to get past that?
     
  14. TantzyGames

    TantzyGames

    Joined:
    Jul 27, 2016
    Posts:
    51
    I was using NGUI tweens to create the flipping animation. The flip sequence lasted X seconds, so I just swapped sprite visibility at X * 0.5.