I’m developing a game for mobile platforms, with touch based input. My game would use swipes to rotate the character in the appropriate directions, but I have a problem with it. My character should only rotate one time in the appropriate direction and it shouldn’t go forward, when rotating, although it happens every time, when it won’t collide with walls.
Here is my rotation code:
public class AnimationController : MonoBehaviour {
int currentRotation = 0;
// Update is called once per frame
void Update () {
currentRotation = (int)transform.eulerAngles.y;
#if UNITY_EDITOR || UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
if (Input.GetButtonDown("right")) {
transform.Rotate(0, 90, 0, 0);
}
if (Input.GetButtonDown("left")) {
transform.Rotate (0, -90, 0, 0);
}
// if (Input.GetButtonDown("up")) {
// gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
// }
// if (Input.GetButtonDown("down")) {
// gameObject.transform.rotation = Quaternion.Euler(0, 180, 0);
// }
#endif
#if UNITY_ANDROID || UNITY_IOS || UNITY_WP8 || UNITY_WP8_1 || UNITY_BLACKBERRY
if ((SwipeManager.swipeDirection == Swipe.Down) && currentRotation == 0) {
transform.Rotate (0, 90, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Down) && currentRotation == 90) {
}
if ((SwipeManager.swipeDirection == Swipe.Down) && currentRotation == 180) {
transform.Rotate(0, 270, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Down) && currentRotation == 270) {
transform.Rotate (0, 180, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Up) && currentRotation == 0) {
transform.Rotate(0, 270, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Up) && currentRotation == 90) {
transform.Rotate(0, 180, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Up) && currentRotation == 180) {
transform.Rotate(0, 90, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Up) && currentRotation == 270) {
}
if ((SwipeManager.swipeDirection == Swipe.Right) && currentRotation == 0) {
}
if ((SwipeManager.swipeDirection == Swipe.Right) && currentRotation == 90) {
transform.Rotate(0, 270, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Right) && currentRotation == 180) {
transform.Rotate(0, 180, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Right) && currentRotation == 270) {
transform.Rotate(0, 90, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Left) && currentRotation == 0) {
transform.Rotate(0, 180, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Left) && currentRotation == 90) {
transform.Rotate(0, 90, 0, 0);
}
if ((SwipeManager.swipeDirection == Swipe.Left) && currentRotation == 180) {
}
if ((SwipeManager.swipeDirection == Swipe.Left) && currentRotation == 270) {
transform.Rotate(0, 270, 0, 0);
}
#endif
}
}
Here is my swipemanager code:
public enum Swipe { None, Up, Down, Left, Right };
public class SwipeManager : MonoBehaviour {
public float minSwipeLength = 50f;
Vector2 firstPressPos;
Vector2 secondPressPos;
Vector2 currentSwipe;
public static Swipe swipeDirection;
void Update () {
DetectSwipe();
}
public void DetectSwipe () {
if (Input.touches.Length > 0) {
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began) {
firstPressPos = new Vector2(t.position.x, t.position.y);
}
if (t.phase == TouchPhase.Ended) {
secondPressPos = new Vector2(t.position.x, t.position.y);
currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
// Make sure it was a legit swipe, not a tap
if (currentSwipe.magnitude < minSwipeLength) {
swipeDirection = Swipe.None;
return;
}
currentSwipe.Normalize();
if (!(secondPressPos == firstPressPos))
{
if (Mathf.Abs(currentSwipe.x) > Mathf.Abs(currentSwipe.y)) {
if (currentSwipe.x < 0) {
swipeDirection = Swipe.Left;
}
else {
swipeDirection = Swipe.Right;
}
}
else {
if (currentSwipe.y < 0) {
swipeDirection = Swipe.Down;
}
else {
swipeDirection = Swipe.Up;
}
}
}
}
} else {
swipeDirection = Swipe.None;
}
}
}
And here is my code which is also on the character (just the mobile part of it):
#if UNITY_ANDROID || UNITY_IOS || UNITY_WP8 || UNITY_WP8_1 || UNITY_BLACKBERRY
if(Input.touchCount > 0) {
// touch on screen
if(Input.GetTouch(0).phase == TouchPhase.Began) {
currentScaleTime = 0;
}
if(Input.GetTouch(0).phase == TouchPhase.Stationary) {
startScale = gameObject.transform.localScale;
endScale = new Vector3(transform.localScale.x + scaleChange, transform.localScale.y - scaleChange, transform.localScale.z);
firstInp = true;
}
if((Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(0).phase == TouchPhase.Canceled) && SwipeManager.swipeDirection == Swipe.None) {
if (perc >= 1) {
anim.Play("Jump");
currentLerpTime = 0;
currentScaleTime = 0;
startPos = player.transform.position;
startScale = gameObject.transform.localScale;
endScale = new Vector3 (1, 1, 1);
}
}
if ((Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(0).phase == TouchPhase.Canceled) && SwipeManager.swipeDirection == Swipe.None && gameObject.transform.position == endPos) {
alreadyPlayedVariable = true;
if (Physics.Raycast(player.transform.position, fwd, out objectHit, moveVector.z)) {
if (objectHit.collider.tag == "HelperWall") {
endPos = player.transform.position + player.transform.rotation * helperWallMoveVector;
endPos.y = 0.35f;
}
else if (objectHit.collider.tag == "Wall" || objectHit.collider.tag == "EnemyStartWall") {
}
else if (objectHit.collider.tag == "StartWall") {
if (startPlatformOff) {
}
else {
endPos = player.transform.position + player.transform.rotation * moveVector;
endPos.y = 0.35f;
}
else {
endPos = player.transform.position + player.transform.rotation * moveVector;
endPos.y = 0.35f;
}
}
else {
endPos = player.transform.position + player.transform.rotation * moveVector;
endPos.y = 0.35f;
}
}
}
#endif
if (firstInp) {
currentLerpTime += Time.deltaTime * lerpSpeed;
perc = currentLerpTime;
//player.transform.position = Vector3.Lerp(startPos, endPos, perc);
gameObject.transform.position = Vector3.Lerp(startPos, endPos, animCurve.Evaluate(perc));
currentScaleTime += Time.deltaTime * scaleSpeed;
scalePerc = currentScaleTime;
playerTransform.transform.localScale = Vector3.Lerp(startScale, endScale, animCurve.Evaluate(perc));
}
if (isSuper == true) {
timeInSuperState += Time.deltaTime;
if (timeInSuperState >= timeTillSuperState) {
isSuper = false;
}
}
else {
timeInSuperState = 0;
}
}
What can be the problem with my code? What can I do to avoid forwarding my character when it only should rotate itself? (I have tested my code on an Android device)
Thanks in advance!