- Home /
Bug? Need to wait about 10 seconds to get back to normal
I create a functionality which is "Swipe to slide the object to L/R". But it only works correctly after 10 seconds since the app loaded (10 seconds counted right after "Unity" icon is gone). Anyone met this problem? Maybe something wrong in my code? Here are my code:
using UnityEngine;
using System.Collections;
public class SwipeMove1 : MonoBehaviour {
public float speed=1.5f;
private Vector2 touchPosBegan;
private Vector2 touchPosEnd;
private float touchPosEndIn3D=0;
private float moveDirection;
private float debug;
//determin go next, previous or stay
private bool enableNext=false;
private bool enablePrevious=false;
private bool enableStay=false;
private bool enableDrag=false;
private float currentObjPosX;
private Vector2 touchDeltaPosition;
public Transform posGoal;
private Vector3 posP0 = new Vector3(0, 0 ,0);
private Vector3 posP1 = new Vector3(-25.0f,0 ,0);
private Vector3 posP2 = new Vector3(-50.0f,0 ,0);
private Vector3 posP3 = new Vector3(-75.0f,0 ,0);
private Vector3 posP4 = new Vector3(-100.0f,0 ,0);
private Vector3 posP5 = new Vector3(-125.0f,0 ,0);
private Vector3 posP6 = new Vector3(-150.0f,0 ,0);
private Vector3 posP7 = new Vector3(-175.0f,0 ,0);
private Vector3 posP8 = new Vector3(-200.0f,0 ,0);
private Vector3 posP9 = new Vector3(-225.0f,0 ,0);
private Vector3 posP10 = new Vector3(-250.0f,0 ,0);
private Vector3 posP11 = new Vector3(-275.0f,0 ,0);
private Vector3 posP12 = new Vector3(-300.0f,0 ,0);
private Vector3 posP13 = new Vector3(-325.0f,0 ,0);
private Vector3 posP14 = new Vector3(-350.0f,0 ,0); //Page ends here
private Vector3 posP15 = new Vector3(-375.0f,0 ,0);
private Vector3 posP16 = new Vector3(-400.0f,0 ,0);
private Vector3 posP17 = new Vector3(-425.0f,0 ,0);
private Vector3 posP18 = new Vector3(-450.0f,0 ,0);
private Vector3 posP19 = new Vector3(-475.0f,0 ,0);
private int whichPage;
private float groupOffset;
private float groupOffsetTemp;
public float remainValue;
public float smoothSpeed=0.1f;
public float smoothTime = 0.3F;
//private Vector3 velocity = Vector3.zero;
// Use this for initialization
void Start ()
{
//remainValue = 12.5f % 25.0f;
}
// Update is called once per frame
void Update ()
{
currentObjPosX = transform.position.x;
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
touchPosBegan = Input.GetTouch(0).position;
else if(Input.GetTouch(0).phase == TouchPhase.Moved)
{
// Get movement of the finger since last frame
touchDeltaPosition = Input.GetTouch(0).deltaPosition;
//P0 to Start Page
if (currentObjPosX>0 && touchDeltaPosition.x>0) GoStart();
//PEnd to Start Page
if (currentObjPosX<-350 && touchDeltaPosition.x<0) GoStart();
// Move object across XY plane by controling the speed, in this case, we are moving the camera
if (currentObjPosX<=0 && currentObjPosX>=-350) //need to be >=-225 otherwise >-225 will let user "stock at that page)
transform.Translate (touchDeltaPosition.x*speed*Time.deltaTime,0,0);
}
else if(Input.GetTouch(0).phase == TouchPhase.Ended)
{
touchPosEnd = Input.GetTouch(0).position;
//Move Next
if (Mathf.Abs(touchPosEnd.x-touchPosBegan.x)>400)
if (touchPosEnd.x-touchPosBegan.x<0)
enableNext = true; //or call Next()
//Move Previous
if (Mathf.Abs(touchPosEnd.x-touchPosBegan.x)>400)
if (touchPosEnd.x-touchPosBegan.x>0)
enablePrevious = true;
//Stay
if (Mathf.Abs(touchPosEnd.x-touchPosBegan.x)<400)
if (touchPosEnd.x-touchPosBegan.x>0 || touchPosEnd.x-touchPosBegan.x<0 || touchPosEnd.x-touchPosBegan.x==0)
enableStay = true;
}
}
if(enableNext)
//if(Mathf.Abs(currentObjPosX)<25)
{
if (currentObjPosX<=0 && currentObjPosX>-25) GoP1();
if (currentObjPosX<=-25 && currentObjPosX>-50) GoP2();
if (currentObjPosX<=-50 && currentObjPosX>-75) GoP3();
if (currentObjPosX<=-75 && currentObjPosX>-100) GoP4();
if (currentObjPosX<=-100 && currentObjPosX>-125) GoP5();
if (currentObjPosX<=-125 && currentObjPosX>-150) GoP6();
if (currentObjPosX<=-150 && currentObjPosX>-175) GoP7();
if (currentObjPosX<=-175 && currentObjPosX>-200) GoP8();
if (currentObjPosX<=-200 && currentObjPosX>-225) GoP9();
if (currentObjPosX<=-225 && currentObjPosX>-250) GoP10();
if (currentObjPosX<=-250 && currentObjPosX>-275) GoP11();
if (currentObjPosX<=-275 && currentObjPosX>-300) GoP12();
if (currentObjPosX<=-300 && currentObjPosX>-325) GoP13();
if (currentObjPosX<=-325 && currentObjPosX>-350) GoP14();
if (currentObjPosX<=-350 && currentObjPosX>-375) GoP15(); //Page end at P15
//if (currentObjPosX<=-375 && currentObjPosX>-400) GoP16();
//if (currentObjPosX<=-400 && currentObjPosX>-425) GoP17();
//if (currentObjPosX<=-425 && currentObjPosX>-450) GoP18();
//if (currentObjPosX<=-450 && currentObjPosX>-475) GoP19();
//if (currentObjPosX<=-475 && currentObjPosX>-500) GoStart();
enableNext = false;
}
if(enablePrevious)
{
if (currentObjPosX<=25 && currentObjPosX>0) GoStart();
if (currentObjPosX<=0 && currentObjPosX>-25) GoP0();
if (currentObjPosX<=-25 && currentObjPosX>-50) GoP1();
if (currentObjPosX<=-50 && currentObjPosX>-75) GoP2();
if (currentObjPosX<=-75 && currentObjPosX>-100) GoP3();
if (currentObjPosX<=-100 && currentObjPosX>-125) GoP4();
if (currentObjPosX<=-125 && currentObjPosX>-150) GoP5();
if (currentObjPosX<=-150 && currentObjPosX>-175) GoP6();
if (currentObjPosX<=-175 && currentObjPosX>-200) GoP7();
if (currentObjPosX<=-200 && currentObjPosX>-225) GoP8();
if (currentObjPosX<=-225 && currentObjPosX>-250) GoP9();
if (currentObjPosX<=-250 && currentObjPosX>-275) GoP10();
if (currentObjPosX<=-275 && currentObjPosX>-300) GoP11();
if (currentObjPosX<=-300 && currentObjPosX>-325) GoP12();
if (currentObjPosX<=-325 && currentObjPosX>-350) GoP13();
if (currentObjPosX<=-350 && currentObjPosX>-375) GoP14();
//if (currentObjPosX<=-375 && currentObjPosX>-400) GoP15();
//if (currentObjPosX<=-400 && currentObjPosX>-425) GoP16();
//if (currentObjPosX<=-425 && currentObjPosX>-450) GoP17();
//if (currentObjPosX<=-450 && currentObjPosX>-475) GoP18();
enablePrevious = false;
}
if(enableStay)
{
if (currentObjPosX<=12.5 && currentObjPosX>-12.5) GoP0();
if (currentObjPosX<=-12.5 && currentObjPosX>-37.5) GoP1();
if (currentObjPosX<=-37.5 && currentObjPosX>-62.5) GoP2();
if (currentObjPosX<=-62.5 && currentObjPosX>-87.5) GoP3();
if (currentObjPosX<=-87.5 && currentObjPosX>-112.5) GoP4();
if (currentObjPosX<=-112.5 && currentObjPosX>-137.5) GoP5();
if (currentObjPosX<=-137.5 && currentObjPosX>-162.5) GoP6();
if (currentObjPosX<=-162.5 && currentObjPosX>-187.5) GoP7();
if (currentObjPosX<=-187.5 && currentObjPosX>-212.5) GoP8();
if (currentObjPosX<=-212.5 && currentObjPosX>-237.5) GoP9();
if (currentObjPosX<=-237.5 && currentObjPosX>-262.5) GoP10();
if (currentObjPosX<=-262.5 && currentObjPosX>-287.5) GoP11();
if (currentObjPosX<=-287.5 && currentObjPosX>-312.5) GoP12();
if (currentObjPosX<=-312.5 && currentObjPosX>-337.5) GoP13();
if (currentObjPosX<=-337.5 && currentObjPosX>-362.5) GoP14();
//if (currentObjPosX<=-362.5 && currentObjPosX>-387.5) GoP15();
//if (currentObjPosX<=-387.5 && currentObjPosX>-412.5) GoP16();
enableStay = false;
}
}
//Fucnctions for moving to each page
public void GoStart()
{
Application.LoadLevel(0);
}
public void GoP0()
{
posGoal.position = posP0;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP1()
{
posGoal.position = posP1;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP2()
{
posGoal.position = posP2;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP3()
{
posGoal.position = posP3;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP4()
{
posGoal.position = posP4;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP5()
{
posGoal.position = posP5;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP6()
{
posGoal.position = posP6;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP7()
{
posGoal.position = posP7;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP8()
{
posGoal.position = posP8;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP9()
{
posGoal.position = posP9;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP10()
{
posGoal.position = posP10;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP11()
{
posGoal.position = posP11;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP12()
{
posGoal.position = posP12;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP13()
{
posGoal.position = posP13;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP14()
{
posGoal.position = posP14;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
public void GoP15()
{
posGoal.position = posP15;
transform.position = Vector3.Lerp(transform.position, posGoal.position, Time.time*smoothSpeed);
}
}
My device: Android Gingerbread 2.3, HTC T-Mobile G2 Unity 3.5
Answer by Berenger · Mar 23, 2012 at 06:27 PM
If you the app is running smoothly after those 10 seconds, then it's something being loaded, or it's another script. I've never done anything on Android/IOS though.
Anyway, your variables posPXX have a pattern of (-25 * i, 0, 0), I'm sure you can improve that. You don't need all those ifs.
Thanks for the advice! From some other user's help, I cleaned up my code in the following. I deleted all unnecessary prefabs, scripts in the scene, but it still need 10 seconds...
To be more specific: I have 2 major functionalities in this code for user, which is user can drag the object >lifted finger to slide to next object. Only "drag" can work correctly right after game loaded. "Slide to next object" always needs wait 10 seconds....
public class Test : $$anonymous$$onoBehaviour {
public float speed=1.5f;
private float currentObjPosX;
private Vector2 touchDeltaPosition;
public float smoothSpeed=0.1f;
public float smoothTime = 0.3F;
private float goalPos;
void Update ()
{
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved)
{
// Get movement of the finger since last frame
touchDeltaPosition = Input.GetTouch(0).deltaPosition;
//P0 to Start Page
if (currentObjPosX>0 && touchDeltaPosition.x>0) GoStart();
//PEnd to Start Page
if (currentObjPosX<-350 && touchDeltaPosition.x<0) GoStart();
// $$anonymous$$ove object across XY plane by controling the speed, in this case, we are moving the camera
if (currentObjPosX<=0 && currentObjPosX>=-350) //need to be >=-225 otherwise >-225 will let user "stock at that page)
transform.Translate (touchDeltaPosition.x*speed*Time.deltaTime,0,0);
currentObjPosX = transform.position.x;
goalPos = $$anonymous$$athf.Round(transform.position.x/25)*25;
}
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
transform.position = Vector3.Lerp(transform.position, new Vector3(goalPos, 0, 0) , Time.time*smoothSpeed);
}
}
//Fucnctions for moving to each page
public void GoStart()
{
Application.LoadLevel(0);
}