Quantcast
Viewing latest article 18
Browse Latest Browse All 70

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(); gameObject.GetComponent().connectedBody = playerHit.rigidbody; playerScript.grabbing = true; playerScript.grabbingTransform = this.transform; moveState = "Up"; } else if (moveState == "Up" && Input.GetButtonDown("Action1")) { Destroy(gameObject.GetComponent()); 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().connectedBody = null; transform.position = playerHit.transform.position + nextDirection; gameObject.GetComponent().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!!

Viewing latest article 18
Browse Latest Browse All 70

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>