- Home /
Question by
aquaphoenix41 · Aug 30, 2019 at 07:52 AM ·
c#3dprogrammingvisual studio
My cooldown method is not working the cube move only once then It won't move anymore
I'm trying to move a cube by rolling its surface As far as I have done It can move just the way I want However, whenever I try to add a cool down for movement It only move once here is my code : public GameObject cube;
public GameObject center;
public GameObject up;
public GameObject down;
public GameObject right;
public GameObject left;
public int step = 9;
public float speed = 0.01f;
public float movementTimer = 0;
float moveDirection;
bool moveAble = true;
void Start()
{
}
void Update() { moveDirection = Random.value;
if(moveAble == true)
{
if (moveDirection > 0 && moveDirection < 0.25)
{
StartCoroutine("MoveUp");
CoolDown();
}
if (moveDirection > 0.25 && moveDirection < 0.5)
{
StartCoroutine("MoveDown");
CoolDown();
}
if (moveDirection > 0.5 && moveDirection < 0.75)
{
StartCoroutine("MoveLeft");
CoolDown();
}
if (moveDirection > 0.75 && moveDirection < 1)
{
StartCoroutine("MoveRight");
CoolDown();
}
}
void CoolDown() { moveAble = false; movementTimer += Time.deltaTime; if(movementTimer >= 3f) { moveAble = true; movementTimer = 0; } }
IEnumerator MoveUp()
{
for (int i = 0; i < (90 / step); i++)
{
cube.transform.RotateAround(up.transform.position, Vector3.right, step);
yield return new WaitForSeconds(speed);
}
center.transform.position = cube.transform.position;
}
//The rest of the codes are just the changes of direction, so I'll exclude them
Comment
Best Answer
Answer by Hellium · Aug 30, 2019 at 08:18 AM
void Update()
{
moveDirection = Random.value;
if(moveAble)
{
if (moveDirection < 0.25f) StartCoroutine(MoveUp());
else if (moveDirection < 0.5f) StartCoroutine(MoveDown());
else if (moveDirection < 0.75f) StartCoroutine(MoveLeft());
else if (moveDirection <= 1f) StartCoroutine(MoveRight());
moveAble = false
}
CoolDown();
}
void CoolDown()
{
movementTimer += Time.deltaTime;
if(movementTimer >= 3f)
{
moveAble = true;
movementTimer = 0;
}
}