Hello,
Every time I swipe my player left/right/up/down my animation is being played after he change his position. I would like to play animation during his position change and I can’t solve the issue. Here’s my code, please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwipeControls : MonoBehaviour
{
public float speed = 5.0f;
private Vector3 startpos; // start position
private Vector3 endpos; //end position
public int pozioma = 0;
public int pionowa = 0;
Animator anim;
void Start()
{
GetComponent<Animator>();
}
void Update()
{
foreach (Touch touch in Input.touches)
{
Vector3 newPosition;
if (touch.phase == TouchPhase.Began)
{
startpos = touch.position;
endpos = touch.position;
}
if (touch.phase == TouchPhase.Moved)
{
endpos = touch.position;
}
if (touch.phase == TouchPhase.Ended)
{
newPosition = transform.position;
if (Mathf.Abs(startpos.y - endpos.y) > Mathf.Abs(startpos.x - endpos.x))
{
if ((startpos.y - endpos.y) > 100 && pionowa > -1) //swipe down
{
pionowa--;
newPosition.y -= speed;
transform.position = newPosition;
anim.SetTrigger("Flydown");
}
if ((startpos.y - endpos.y) < -100 && pionowa < 1) //swipe up
{
pionowa++;
newPosition.y += speed;
transform.position = newPosition;
anim.SetTrigger("Flyup");
}
}
else
{
if ((startpos.x - endpos.x) > 100 && pozioma > -1) //swipe left
{
pozioma--;
newPosition.z -= speed;
transform.position = newPosition;
anim.SetTrigger("Flyleft");
}
}
if ((startpos.x - endpos.x) < -100 && pozioma < 1) //swipe right
{
pozioma++;
newPosition.z += speed;
transform.position = newPosition;
anim.SetTrigger("Flyright");
}
}
}
}
}