- Home /
Putting Cooldown for flipping and moving the cube
Hello, i need a help about delaying a movement. I have a script about flipping the cube and moving it at the same time i just want to put a cooldown to a movement so the player can't spam it. I think i need help in IEnumerator part. I hope you can help me...
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour { private Vector3 offset; public GameObject player;
public GameObject center;
public GameObject up;
public GameObject down;
public GameObject left;
public GameObject right;
public int step = 9;
public float speed = (float)0.01f;
public bool input = true;
private Limit lc;
public int damageX = 1;
public int damageZ = 1;
public int semiX = 10;
public int semiZ = 10;
public int M1X = 1;
public int M1Z = 1;
void Start()
{
}
// Update is called once per frame
void Update()
{
if (input == true){
if(Input.GetKey(KeyCode.W)){
StartCoroutine("MoveUp");
semiZ--;
input = false;
}if(Input.GetKey(KeyCode.S)){
StartCoroutine("MoveDown");
semiZ--;
input = false;
}if(Input.GetKey(KeyCode.A)){
StartCoroutine("MoveLeft");
semiX--;
input = false;
}if(Input.GetKey(KeyCode.D)){
StartCoroutine("MoveRight");
semiX--;
input = false;
}
}if(semiX <=9){
lc = GameObject.Find("Player").GetComponent<Limit>();
lc.x -= damageX;
semiX++;
}if(semiZ <=9){
lc = GameObject.Find("Player").GetComponent<Limit>();
lc.z -= damageZ;
semiZ++;
}
}
// I need help in this part of the script//
IEnumerator MoveUp(){
for (int i = 0; i < (90 / step); i++){
player.transform.RotateAround(up.transform.position, Vector3. right,step);
yield return new WaitForSeconds (speed);
}
center.transform.position = player.transform.position;
input = true;
}
IEnumerator MoveDown(){
for (int i = 0; i < (90 / step); i++){
player.transform.RotateAround(down.transform.position, Vector3. left,step);
yield return new WaitForSeconds (speed);
}
center.transform.position = player.transform.position;
input = true;
}
IEnumerator MoveRight(){
for (int i = 0; i < (90 / step); i++){
player.transform.RotateAround(right.transform.position, Vector3. back,step);
yield return new WaitForSeconds (speed);
}
center.transform.position = player.transform.position;
input = true;
}
IEnumerator MoveLeft(){
for (int i = 0; i < (90 / step); i++){
player.transform.RotateAround(left.transform.position, Vector3. forward,step);
yield return new WaitForSeconds (speed);
}
center.transform.position = player.transform.position;
input = true;
}
}
You need to set input
to false, at the beginning of each coroutine. This way, the Update method won't be able to register input until the coroutine has finished executing, where input is true again.
Answer by Llama_w_2Ls · Jun 03, 2021 at 12:57 PM
Also, you could shorten your script to use objects instead of if blocks, if you wanted:
public class Movement : MonoBehaviour
{
//...
public bool input = true;
Dictionary<KeyCode, ObjectDirection> Coroutines = new Dictionary<KeyCode, ObjectDirection>()
{
{ KeyCode.W, new ObjectDirection(Vector3.right, up, semiZ) },
{ KeyCode.S, new ObjectDirection(Vector3.left, down, semiZ) },
{ KeyCode.D, new ObjectDirection(Vector3.right, back, semiX) },
{ KeyCode.A, new ObjectDirection(Vector3.left, forward, semiX) }
};
void Update()
{
if (input == true)
{
foreach (var coroutine in Coroutines)
{
if (Input.GetKey(coroutine.Key))
{
StartCoroutine(Move(coroutine.Value));
coroutine.Value.Semi--;
}
}
}
if (semiX <= 9)
{
lc = GameObject.Find("Player").GetComponent<Limit>();
lc.x -= damageX;
semiX++;
}
if (semiZ <= 9)
{
lc = GameObject.Find("Player").GetComponent<Limit>();
lc.z -= damageZ;
semiZ++;
}
}
IEnumerator Move(ObjectDirection dir)
{
input = false;
for (int i = 0; i < (90 / step); i++)
{
player.transform.RotateAround(dir.Object.transform.position, dir.Direction, step);
yield return new WaitForSeconds(speed);
}
center.transform.position = player.transform.position;
input = true;
}
}
public class ObjectDirection
{
public Vector3 Direction;
public GameObject Object;
public int Semi;
public ObjectDirection(Vector3 dir, GameObject obj, int semi)
{
Direction = dir;
Object = obj;
Semi = semi;
}
}