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 KathrynHazuka · Dec 25, 2015 at 03:06 PM · getbuttondownexecutiontwice

GetButtonDown() execusting twice

Here is the part of the code that is behaving abnormally:

 void GetGrabbed(RaycastHit playerHit, Vector3 direction)
 {
     if (moveState == "Down" && Input.GetButtonDown("Action1"))
     {
         print("Hi");

         transform.position = playerHit.transform.position + direction;
         gameObject.AddComponent<FixedJoint>();
         gameObject.GetComponent<FixedJoint>().connectedBody = playerHit.rigidbody;
         playerScript.grabbing = true;
         playerScript.grabbingTransform = this.transform;
         moveState = "Up";
     }
     else if (moveState == "Up" && Input.GetButtonDown("Action1"))
     {
         Destroy(gameObject.GetComponent<FixedJoint>());
         playerScript.grabbing = false;
         moveState = "Down";
     }
     if (Input.GetButtonDown("Action2"))
     {
         if (moveState == "Up")
         {
             Vector3 nextDirection = new Vector3(0, 0, 0);
             print(this.redirectorID);
             if (direction == new Vector3(0, 0, 1))
                 nextDirection = new Vector3(1, 0, 0);
             if (direction == new Vector3(1, 0, 0))
                 nextDirection = new Vector3(0, 0, -1);
             if (direction == new Vector3(0, 0, -1))
                 nextDirection = new Vector3(-1, 0, 0);
             if (direction == new Vector3(-1, 0, 0))
                 nextDirection = new Vector3(0, 0, 1);
             gameObject.GetComponent<FixedJoint>().connectedBody = null;
             transform.position = playerHit.transform.position + nextDirection;
             gameObject.GetComponent<FixedJoint>().connectedBody = playerHit.rigidbody;
         }
         else if (moveState == "Down")
         {
             transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y + 90f, 0);
             print("Hi");
         }
     }
 }

the problematic code is from line:

if (Input.GetButtonDown("Action2"))

to lines:

gameObject.GetComponent().connectedBody = playerHit.rigidbody;

}

When ran in the game, the code executes the GetButtonDown("Action2") twice. This only happens when both the

transform.position = playerHit.transform.position + nextDirection;

and the

*if (direction == new Vector3(0, 0, 1))

nextDirection = new Vector3(1, 0, 0);* (or any other direction change)

lines of code are executed.

When the object is moved to another side of the player, the 4 raycasts from the object being moved detect that the player is now in another position. This position change is in the clockwise rotation. When holding the object and pressing Action2, the nextDirection is calculated based on the current detected direction and moves the object to the correct position. At that point nothing else should happen as GetButtonDown("Action2") should only execute ONCE. but in this case, after the box moves, the code is ran through again and for no logical reason it runs through the GetButtonDown("Action2") ONE MORE TIME. In this setup, it never runs once, nor more than twice, but EXACLY TWICE each time. GetButtonUp("Action2") produces the same result when replacing GetButtonDown("Action2").

I have tried everything and i can not solve this problem. This seams illogical to me as GetButtonDown() and GetButtonUp() should run only ONE time.

This Code is ran inside the Update() function as do any of my input detections.

I have no energy to solve this... and since it does not follow normal logic that any GetButtonDown()/Up() has followed so far i can not comprehend a solution for it... but i know the problem is the fact that the object moves and recalculates the new position... I just don't know why that is causing GetButtonDown() to run through twice...

Please Help!!

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

0 Replies

· Add your reply
  • Sort: 

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

32 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

Related Questions

add commands to execution list and execute them 1 Answer

¿PLAY and Pause with Gear Vr button Video 360? 0 Answers

Looking for a specific page in documentation for Unity3D 0 Answers

Input.GetButtonDown doesn't want to work 1 Answer

Game working perfectly in editor but not in build? 2 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