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");
}
}
}
}