Reputation: 113
I have one object in my scene, when mouse dragging up, I want to instantiate a new object above the existing one, and if the dragging up still happening, I want to instantiate a third object, and the same thing for the fourth. But if for example the dragging up end immediately when the second object spawned, the next objects should not be spawned until next dragging up.
And the opposite for mouse dragging down, first, the fourth object (in the top) should be deleted, and when still dragging down the third object should also be deleted etc... until the first object. And also if the dragging down end immediately when the fourth object has been deleted, the next objects should not be deleted until next dragging down.
Here's the code I wrote for the dragging up mechanic, but when dragging up, all the 3 objects are spawned in the same time! and even when releasing the drag, the next one are also spawned!
public GameObject SpawnedBallUp1;
public GameObject SpawnedBallUp2;
public GameObject SpawnedBallUp3;
float scaleFactorY;
float DragDistance;
bool MovingUp = false;
bool MovingDown = false;
bool BallUp1Done = false;
bool BallUp2Done = false;
bool BallUp3Done = false;
void Update () {
if (Input.GetMouseButton(0) && !Input.GetMouseButtonDown(0) || Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved && !(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
{
scaleFactorY = Input.GetAxis("Mouse Y") * 10;
if (scaleFactorY < 0)
{
if (!MovingDown)
{
MovingDown = true;
}
MovingUp = false;
}
else if (scaleFactorY > 0)
{
if (!MovingUp)
{
MovingUp = true;
if (!BallUp1Done)
{
BallUp1Done = true;
StartCoroutine(BallUp1());
if (!BallUp2Done)
{
BallUp2Done = true;
StartCoroutine(BallUp2());
if (!BallUp3Done)
{
BallUp3Done = true;
StartCoroutine(BallUp3());
}
}
}
}
MovingDown = false;
}
else if(scaleFactorY < 0.0005f && DragDistance > -0.0005f)
{
if (MovingUp)
{
MovingUp = false;
}
if (MovingDown)
{
MovingDown = false;
}
}
}
private IEnumerator BallUp1()
{
yield return new WaitForSeconds(0.3f);
if(BallUp1Done)
{
GameObject Ball1 = Instantiate(SpawnedBallUp1, transform.position, Quaternion.identity);
Ball1.transform.parent = transform.parent;
}
}
private IEnumerator BallUp2()
{
yield return new WaitForSeconds(0.6f);
if (BallUp1Done)
{
GameObject Ball2 = Instantiate(SpawnedBallUp2, transform.position, Quaternion.identity);
Ball2.transform.parent = transform.parent;
}
}
private IEnumerator BallUp3()
{
yield return new WaitForSeconds(0.9f);
if (BallUp1Done)
{
GameObject Ball3 = Instantiate(SpawnedBallUp3, transform.position, Quaternion.identity);
Ball3.transform.parent = transform.parent;
}
}
Upvotes: 0
Views: 46
Reputation: 1180
I can answer half of your question, I think. The reason why "even when releasing the drag, the next one are also spawned!" is that while you are appropriately waiting for a set time inside the coroutines, after those WaitForSeconds
calls, you are not checking to make sure the user is still dragging. This means that as long as the user was dragging when the coroutines were started, the objects will definitely be spawned.
To fix this, add a check inside the coroutines like if (MovingUp)
, or if it's important that it only occurs as part of a single drag, maybe give each drag an ID when it begins, and then check that you're still in the same drag by comparing drag IDs just before the balls are spawned.
I'm not sure why they all spawn simultaneously -- based on those Coroutines, they shouldn't, unless your framerate is super low and you're only getting like 1 frame per second.
Upvotes: 1