x


Touches don't respond correct

Hey Guys,

so I'm trying to work with Unity iPhone, i got Unity-Remote so i could test the gameplay on my device. Here is my Project-Overview:

  • Plattforming/ SideScrolling Game
  • working with ex2D (Spritemanager-PlugIn, works great so far!)
  • I have a character with no scripts attached (okay, that ex2D script...)
  • That character has also one ex2D-Animation (it's a Move-Animation, Wrapmode is selected as "LOOP")
  • I made two Buttons (made them in Illustrator, exported them as transparent .png files), and using them as GUITexture-Files on the screen itself

So, the Problem is:

i made two scripts for each button with nearly 1:1 code. The Left-Button should move my Playersprite to the left, the Right-Button should move my Playersprite to the right. This is working. My idea is, AFTER i press one of the buttons, the Character starts to move AND the animation is played. I'm trying to work with the Touch-Phases, but i don't get a solution i am satisfied with, because sometimes, my device doesn't recognize a certain phase. When i press the button it's moving all along, that's fine (although, the animation is stuck in the first frame and plays the whole animation after releasing my finger from the screen <- how can i solve this "Sideproblem"). When i release my finger, it sometimes doesn't recognize the Touch-Phase has ended. I made some Debug.Logs to figure out when something happens. I am sitting three days for now in front of this problem, looked up the internet, tried some other "solutions" in order to get nice touch-handling but nothing worked properly, it's devastating, so i beg you, could someone please help?

Of course here is my code so far:

//public
public float speed = 10.0f;

//private
int touchCount = 0;
    playingAnim = false;

// Objects
GameObject player; 


void Start () {
    player = GameObject.Find("Ritter");

}

// Update is called once per frame
void FixedUpdate () {

    moving();   
}
void moving()
{

    foreach(Touch touch in Input.touches)
    {

       if(guiTexture.HitTest(touch.position))
       {
         Debug.Log("PRESSED");

         if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Stationary)
         {
          player.transform.Translate(speed*Time.fixedDeltaTime, 0,0);
          player.GetComponent<exSpriteAnimation>().Play("RitterMove", 0);

          playingAnim = true;

          Debug.Log("PHASE BEGAN - RIGHT");

         }
         else if(touch.phase == TouchPhase.Stationary)
         {
          player.transform.Translate(speed*Time.fixedDeltaTime, 0,0);
            player.GetComponent<exSpriteAnimation>().Play("RitterMove");

          player.transform.Translate(speed*Time.fixedDeltaTime, 0,0);

          playingAnim = true;


          Debug.Log("PHASE STATIONARY - RIGHT");

         }

         else if(touch.phase == TouchPhase.Ended)
         {
          playingAnim = false;

          if(playingAnim == false)
          {
              player.GetComponent<exSpriteAnimation>().IsPlaying("RitterMove");
              player.GetComponent<exSpriteAnimation>().Stop();

          }

          Debug.Log("ENDE DES BETATSCHENS! - RIGHT");
         }
       }
    }


}
more ▼

asked May 04 '12 at 09:31 PM

Mayhem gravatar image

Mayhem
13 7 9 14

Did you ever get a satisfactory solution to this issue? I've been having a very similar problem for a while now and it is killing my project.

Jun 19 '12 at 08:04 PM Steveosaurous

Well I can tell you that I had to write my own wrapper that sorted out whether a touch phase had ended or begun because I couldn't get the right values. A missing touch fires a cancelled event for me etc.

Jun 19 '12 at 09:05 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

It looks like playingAnim = false; Should be positioned after the condition that checks if it is false.

if(playingAnim == false) { ... }

playingAnim = false;

That should allow you to stop the animation... the side problem.


For the main issue, could it be that your code is misinterpreting the canceled phase as a begin phase?

http://unity3d.com/support/documentation/ScriptReference/TouchPhase.html

You might be able to confirm if you explicitly handle each touch phase possibility, there are 5, and print a debug log statement for each distinct phase.

more ▼

answered Jun 19 '12 at 10:12 PM

awaxnova gravatar image

awaxnova
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3739
x2000
x580
x30
x25

asked: May 04 '12 at 09:31 PM

Seen: 752 times

Last Updated: Jun 19 '12 at 10:12 PM