• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by GiMark88 · Jun 03, 2021 at 09:53 AM · movementcubedelayflippingcooldown

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

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Llama_w_2Ls · Jun 03, 2021 at 12:44 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GiMark88 · Jun 06, 2021 at 12:35 PM 0
Share

Thank you it works sorry for my late reply.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I make my character run over a cube? 0 Answers

Gun Animations Aren't Playing Right Away? 2 Answers

How can I make an object follow another's path with some time delay? 2 Answers

Cube roll over hills/objects 2 Answers

moving spheres on a plate 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges