How can I avoid Input.GetKeyDown() being detected before it complete the iTween.MoveAdd() call?
I want to make a lane swapping attribute in a GameObject.
I made a class Player
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
private bool rightBlock;
private bool leftBlock;
private bool toChangeLane;
private bool toLeft;
private bool toRight;
private int lane;
private static bool isMoving;
void Awake ()
{
lane = 0;
isMoving = false;
}
void Start ()
{
rightBlock = false;
leftBlock = false;
toChangeLane = false;
toLeft = false;
toRight = false;
}
public void swapRight ()
{
if (lane == 0 || lane == -1) {
isMoving = true;
iTween.MoveAdd (gameObject, iTween.Hash ("x", 3, "easeType", "spring", "time", .4));
lane = lane + 1;
Debug.Log ("Lane = " + lane);
}
}
public void swapLeft ()
{
if (lane == 0 || lane == 1) {
iTween.MoveAdd (gameObject, iTween.Hash ("x", -3, "easeType", "spring", "time", .4));
lane = lane - 1;
Debug.Log ("Lane = " + lane);
}
}
}
and a class PlayerControl
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerControl : MonoBehaviour
{
private Player pl;
private void Start ()
{
pl = GetComponent<Player> ();
}
private void Update ()
{
if (Input.GetKeyDown ("left")) {
pl.swapLeft ();
}
else if (Input.GetKeyDown ("right"))
pl.swapRight ();
}
}
I made 3 lanes
-1=Left 0= Middle 1 = Right
I am moving the player with iTween.MoveAdd() function. But the problem is when I press the left/right keyboard button twice very quickly, it makes the lane=0 to lane=-1/1 where I wanted to do it when the iTween.MoveAdd() function finishes it's work, like as- when I press right(initially lane=0) it will first Move the Player GameObject then it will turn it to 1 if then I press left it will turn into 0 and then again pressing left will make it -1.
like the image below with a Debugged Log of lane variable.
But it is responding when the button is pressed twice very fast.
The question is,
How can I make the lane variable modified after the iTween finishes it's work
Thanks in advance
Answer by hexagonius · Jan 16, 2016 at 02:37 PM
There must be something in the iTween API that tells you when an animation finishes, either an event or a method you can poll (preferably the first one).
You can create a moving boolean. Set it to true when any animation starts. Set it to false when the animation ends. In Update return immediately if moving is true.
Answer by SouravOrion · Jan 17, 2016 at 05:09 AM
I tried calling a function moveBoolFalse when the animation is complete from the iTween API's oncomplete call. The code is
public void moveBoolFalse ()
{
isMoving = false;
Debug.Log ("IN on complete" + isMoving + lane);
}
public void swapRight ()
{
if ((lane == 0 || lane == -1) && (isMoving == false)) {
isMoving = true;
iTween.MoveAdd (gameObject, iTween.Hash ("x", 3, "easeType", "spring", "time", .4, "oncomplete", "moveBoolFalse"));
lane = lane + 1;
Debug.Log ("Lane = " + lane + " " + isMoving);
}
}
though it is not working. Don't know where I am doing the mistake @hexagonius