0

Try to detect if player's touch is inside the play area, and if so, start a timer. I already have the starting a timer part working for keyboard input, just not touch (mobile) input.

if(...) {
    timer.Start();
}

My game board is a Rect Transform with a sprite:

enter image description here

EDIT:

I tried what's in the duplicate but it still didn't work.

public class PlayerController : MonoBehaviour, IBeginDragHandler 
{
    void Start() 
    {
        ...
        // For touch controls
        addPhysics2DRaycaster();
    }

    // For touch controls
    void addPhysics2DRaycaster()
    {
        Physics2DRaycaster physicsRaycaster = GameObject.FindObjectOfType<Physics2DRaycaster>();
        if (physicsRaycaster == null)
        {
            Camera.main.gameObject.AddComponent<Physics2DRaycaster>();
        }
    }
}

Here is where I try to start the timer:

public void OnBeginDrag(PointerEventData eventData)
{
    timer.Start();
}

Here is the code for the controls:

// FixedUpdate() - called before physics calculations
void FixedUpdate() {
    // Touch swipe input
    if(controlToggle.isOn) {
        joystick.SetActive(false);
        controlToggle.GetComponentInChildren<Text>().text = "Swipe";

        if(Input.touchCount > 0) {
            Touch myTouch = Input.touches[0];

        if(myTouch.phase == TouchPhase.Began) {
            touchOrigin = myTouch.position;
        } else if(myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0) {
            Vector2 touchEnd = myTouch.position;
            float x = touchEnd.x - touchOrigin.x;
            float y = touchEnd.y - touchOrigin.y;
            touchOrigin.x = -1;
            float moveHorizontal = x;
            float moveVertical = y;
            Vector2 movement = new Vector2(moveHorizontal, moveVertical);
            rb2d.AddForce(movement * speed);
        }
    }
}

...
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
Casey
  • 536
  • 2
  • 14
  • 28
  • What do you mean by "certain area"? Want to detect click on a sprite? – Programmer Dec 10 '17 at 19:07
  • Yes, the sprite is the game board area – Casey Dec 10 '17 at 19:31
  • Although its more of a swipe than a click if that makes a difference – Casey Dec 10 '17 at 19:32
  • Alright. See #7 from the duplicate since this is a 2D collider. – Programmer Dec 10 '17 at 19:51
  • I followed it as best I could, but it still isn't working. Added the new code to the question @Programmer – Casey Dec 11 '17 at 01:36
  • Read the code again. You have to implement `IBeginDragHandler` if you want to use `OnBeginDrag`. Read it again. – Programmer Dec 11 '17 at 04:25
  • I added it, but my timer still doesn't start when I do a drag anywhere on the screen – Casey Dec 11 '17 at 05:57
  • I also don't see where I reference the sprite I'm trying to target @Programmer – Casey Dec 11 '17 at 06:00
  • You tried something and it doesn't work? Update your question with your new code. By the way, you supposed to add "EDIT" then your new code so that your original code will be conflict with your Edit – Programmer Dec 11 '17 at 09:49
  • Ok, I added the one class you talked about to my code `IBeginDragHandler`. And idk what you mean by add "EDIT", I don't understand your "english". @Programmer – Casey Dec 11 '17 at 18:12
  • I rolled it back for you and fixed the "EDIT" stuff I talked about. Now you should understand how to properly edit your question. Note that script in your question must be attached to the object you want to detect clicks on. Put `Debug.Log` inside the `OnBeginDrag` function and see if it gets called. Also, remove canvas from your sprite object. That is unnecessary. Canvas is used for UI Objects not for Sprite Renderer. – Programmer Dec 11 '17 at 18:46
  • Since my timer is attached to a different object than where I want to detect click, how do I access the timer from another controller? @Programmer – Casey Dec 14 '17 at 06:53

0 Answers0