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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Simple cell-based game goes bonkers when clicked on the edge of the cell

Discussion in '2D' started by Tigro, Jul 5, 2020.

  1. Tigro

    Tigro

    Joined:
    Apr 25, 2014
    Posts:
    58
    I'm creating a simple casual game in which you click on cells to change their colours according to some rules. I'm using tweening to make the tapping animations responsive and it's all cool but when you click right on the edge of the cell (have to do it on a phone, can't replicate it with a mouse), the cell gets weirdly "stuck" in this loop where it just goes smaller and larger repeatedly (ie. firing the onMouseEnter() and onMouseExit() tweens) for some time until it comes to a halt.

    This is the code doing the tweening when you hover the cell or exit it:

    Code (CSharp):
    1.     void OnMouseEnter()
    2.     {
    3.         centerPivot.DOScale(.8f, .1f).SetId(555);
    4.         Debug.Log(this.name + " entered");
    5.  
    6.     }
    7.  
    8.     void OnMouseExit()
    9.     {      
    10.         centerPivot.DOScale(1f, .3f);
    11.         Debug.Log(this.name + " exited");
    12.  
    13.     }
    Here's a vide of the problem in action (first the correct clicking is shown and then what happens when you click on the edge - and you can see in the console log that the logs for entering and quitting the cell are going on and on until you click outside of it):


    Why is that so? I've tried tons of things, like giving the tweens IDs and killing them or introducing states to the code but it doesn't help a bit, it's as if the code catches the finger clicking the edge of the cell on multiple frames and then fires the respective onMouseEnter() and OnMouseExit()'s discarding that the enter and exit had already happened instead of just doing: a finger on the edge happened -> OnMouseEnter() -> the cell gets slighlty smaller due to the tween but the finger is already lifted off -> OnMouseExit().

    Instead of this, it looks like it's going:caught the finger on the edge on 60 frames so let's do the OnMouseEnter() -> OnMouseExit() cycle for each of the qualifying frames where the finger stood on the screen even though it's been ages since it's off.

    Any ideas?
     
  2. Tigro

    Tigro

    Joined:
    Apr 25, 2014
    Posts:
    58
    No ideas? :(