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;
}
}