- Home /
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");
}
}
}
}
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.
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.
Answer by awaxnova · Jun 19, 2012 at 10:12 PM
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.
Answer by kreso · Sep 28, 2014 at 06:16 AM
This is a late answer I know, but I had same problem as well. I hope this helps someone else. The solution was to move the code from FixedUpdate() to Update() - that way ended phase was recognized correctly every time.
Thanks @kreso, this solution worked for me. It seems to work now.
Your answer
Follow this Question
Related Questions
All objects response to one touch 1 Answer
Unity iPhone scene transition on Tap of GUIText 1 Answer
The iPhoneInput.touchCount is 5 while more or no touch actually. 1 Answer
I want to destroy the top object from a stack of objects on touch, not all objects. 1 Answer
Multitouch problem in Unity iPhone 2 Answers