Question by
mrmichaelb · Apr 11, 2018 at 10:14 PM ·
coroutinecoroutinesupdate functionasynchronouscoroutine errors
Unity C# Punching Coroutine not completing
So I have the code as follows.
void Update() {
// WASD Movement
if (Input.GetKey(KeyCode.W)) { movementUpdate.y += movementPerSecond * Time.deltaTime; }
if (Input.GetKey(KeyCode.A)) { movementUpdate.x -= movementPerSecond * Time.deltaTime; }
if (Input.GetKey(KeyCode.S)) { movementUpdate.y -= movementPerSecond * Time.deltaTime; }
if (Input.GetKey(KeyCode.D)) { movementUpdate.x += movementPerSecond * Time.deltaTime; }
playerBody.position = playerBody.position + movementUpdate;
movementUpdate = new Vector3(0, 0, 0);
// Body rotation relative to cursor position
Vector3 dir = Input.mousePosition - Camera.main.WorldToScreenPoint(transform.position);
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
playerBody.rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
// hand position via function
UpdateHandPosition(playerLeftHand, desiredPositionForLeftHand);
UpdateHandPosition(playerRightHand, desiredPositionForRightHand);
if (Input.GetMouseButtonDown(0)) {
Punch();
}
}
...
void Punch() {
StartCoroutine(PunchCoroutine());
}
IEnumerator PunchCoroutine() {
Debug.Log("User punched.");
if (onLeftHandPunch = false) {
print(onLeftHandPunch);
onLeftHandPunch = false;
// punch the shit
desiredPositionForLeftHand = "middleMiddleCoordinates";
yield return new WaitForSeconds(1f);
Debug.Log("Corountine finished.");
desiredPositionForLeftHand = "topMiddleCoordinates";
} else {
print(onLeftHandPunch);
onLeftHandPunch = true;
// punch the shit
desiredPositionForRightHand = "middleMiddleCoordinates";
}
}
But when I play the code, and click the left mouse button, the following is displayed in the console.
User punched.
False
The Coroutine won't finish and display
Coroutine finished.
Can somebody help me out here?
Comment
Your answer
Follow this Question
Related Questions
Add points every five seconds 1 Answer
Coroutine cancels for no reason 0 Answers
Why isn't my coroutine working when I call it from another script. 0 Answers
Coroutine Not working properly. 1 Answer