Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by SouravOrion · Jan 16, 2016 at 01:06 PM · scripting problem

How can I avoid Input.GetKeyDown() being detected before it complete the iTween.MoveAdd() call?

I want to make a lane swapping attribute in a GameObject.

I made a class Player

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour
 {
         
     private bool rightBlock;
     private bool leftBlock;
     private bool toChangeLane;
     private bool toLeft;
     private bool toRight;
     private int lane;
 
     private static bool isMoving;
 
     void Awake ()
     {
         lane = 0;
         isMoving = false;
     }
 
     void Start ()
     {
         rightBlock = false;
         leftBlock = false;
         toChangeLane = false;
         toLeft = false;
         toRight = false;
 
 
     }
 
 
 
 
 
 
 
     public void swapRight ()
     {
 
 
         if (lane == 0 || lane == -1) {
             isMoving = true;
 
                 iTween.MoveAdd (gameObject, iTween.Hash ("x", 3, "easeType", "spring", "time", .4));
                 lane = lane + 1;
                 Debug.Log ("Lane = " + lane);
 
 
             
         }
         
     }
 
     public void swapLeft ()
     {
 
         if (lane == 0 || lane == 1) { 
                 
             iTween.MoveAdd (gameObject, iTween.Hash ("x", -3, "easeType", "spring", "time", .4));
             lane = lane - 1;
             Debug.Log ("Lane = " + lane);
 
 
         }
 
     }
 
 }
 

and a class PlayerControl

 using System;
 using UnityEngine;
 using UnityStandardAssets.CrossPlatformInput;
 
 
 public class PlayerControl : MonoBehaviour
 {
     private Player pl;
 
     private void Start ()
     {
         pl = GetComponent<Player> ();
 
 
 
     }
         
         
     private void Update ()
     {
             
         if (Input.GetKeyDown ("left")) {
 
             pl.swapLeft ();
 
         }
         else if (Input.GetKeyDown ("right"))
             pl.swapRight ();
             
             
             
     }
         
         
         
 }
 
 

I made 3 lanes

-1=Left 0= Middle 1 = Right

I am moving the player with iTween.MoveAdd() function. But the problem is when I press the left/right keyboard button twice very quickly, it makes the lane=0 to lane=-1/1 where I wanted to do it when the iTween.MoveAdd() function finishes it's work, like as- when I press right(initially lane=0) it will first Move the Player GameObject then it will turn it to 1 if then I press left it will turn into 0 and then again pressing left will make it -1.

like the image below with a Debugged Log of lane variable. alt text alt text

But it is responding when the button is pressed twice very fast.

The question is,

How can I make the lane variable modified after the iTween finishes it's work

Thanks in advance

left.png (73.9 kB)
mid.png (74.3 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Jan 16, 2016 at 02:37 PM

There must be something in the iTween API that tells you when an animation finishes, either an event or a method you can poll (preferably the first one).
You can create a moving boolean. Set it to true when any animation starts. Set it to false when the animation ends. In Update return immediately if moving is true.

Comment
Add comment · 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
0

Answer by SouravOrion · Jan 17, 2016 at 05:09 AM

I tried calling a function moveBoolFalse when the animation is complete from the iTween API's oncomplete call. The code is

 public void moveBoolFalse ()
     {
         isMoving = false;
         Debug.Log ("IN on complete" + isMoving + lane);
 
     }
 
 
 
 
 
     public void swapRight ()
     {
 
 
         if ((lane == 0 || lane == -1) && (isMoving == false)) {
             isMoving = true;
 
             iTween.MoveAdd (gameObject, iTween.Hash ("x", 3, "easeType", "spring", "time", .4, "oncomplete", "moveBoolFalse"));
             lane = lane + 1;
             Debug.Log ("Lane = " + lane + " " + isMoving);
 
 
 
 
 
         }
         
     }

though it is not working. Don't know where I am doing the mistake @hexagonius

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

41 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

Related Questions

help! i am a newbie here and i got a problem in scripting 0 Answers

Why Collider2D size Y changes affects child object position 0 Answers

2D Projectile Script Not Working 1 Answer

How to Undo a lot of created objects at once? [Solved] 1 Answer

AI and player guns not fireing, have i missed a step here? error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement 1 Answer


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