Coroutine endlessly looping?
Hello all. I am new with Unity and am currently making a basic character controller for a player. The problem I am having is with my jump function that I have created. I am now using an IEnumerator for the jump process, but for some reason it is re-looping itself endlessly after the first yield return. I have spent the last hour trying to find what is causing this, but there is nothing in my code that re-calls the coroutine. When I went through the code step by step, it just gets to the first return, then goes straight back in again, for some unknown reason. I have left attached my whole controller and if anybody can see what I am missing, the help would be much appreciated. Many thanks!
public class PlayerController : MonoBehaviour {
#region Variables
[SerializeField]
GameObject Cam;
public float walkSpeed = 2f;
public float runSpeed = 6f;
public float jumpHeight = 20f;
CapsuleCollider coll;
Rigidbody rb;
public LayerMask groundLayers;
public float turnSmoothTime = 0.2f; //seconds for smooth damp to go from current value to target value
float turnSmoothVelocity;
public float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
bool isAttacking = false;
bool isJumping = false;
bool isFalling = false;
float nextAttackTime = 0f;
[SerializeField]
float attackFreq = 0.5f;
float nextJumpTime = 0f;
[SerializeField]
float jumpFreq = 0.5f;
#endregion
Animator animator; //attach animator in editor
void Start () {
animator = GetComponent<Animator>();
coll = GetComponent<CapsuleCollider>();
rb = GetComponent<Rigidbody>();
}
IEnumerator Jump()
{
//Debug.Log("Jump called");
animator.SetTrigger("isJumping");
rb.velocity += jumpHeight * Vector3.up;
yield return new WaitUntil(() => rb.velocity.y <= 10);
isFalling = true;
animator.SetTrigger("isFalling");
yield return new WaitUntil(() => isGrounded());
animator.SetTrigger("hasLanded");
isJumping = false;
nextJumpTime = Time.time + jumpFreq;
}
void Attack() //take in number for different attack and use if statements for playing different animations
{
isAttacking = true;
animator.Play("Unarmed-Attack-L3");
nextAttackTime = Time.time + attackFreq;
isAttacking = false;
}
void Inputs()
{
if (Input.GetAxisRaw("AttackL") > 0 & !isAttacking & Time.time >= nextAttackTime)
{
Attack();
}
if (Input.GetAxisRaw("Jump") > 0 & !isJumping & Time.time >= nextJumpTime)
{
if (isGrounded())
{
isJumping = true;
StartCoroutine(Jump());
}
}
}
void Update()
{
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); //User input for movement
Inputs();
if (input != Vector2.zero)
{
float targetRotation = Mathf.Atan2(input.x, input.y) * Mathf.Rad2Deg;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(GameObject.FindGameObjectWithTag("MainCamera").transform.eulerAngles.y + targetRotation, targetRotation, ref turnSmoothVelocity, turnSmoothTime); //creates a smooth turning for character
}
bool running = (Input.GetKey(KeyCode.LeftShift) | (Input.GetAxisRaw("Special") > 0)); //Shift must be held to allow running speed
float targetSpeed = ((running) ? runSpeed : walkSpeed) * input.magnitude; //if running, speed = run speed, otherwise speed = walk speed if magnitude = 0, speed will be 0
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime); //adds smoothing between movement states
transform.Translate(((Cam.transform.forward * input.normalized.y * currentSpeed) + (Cam.transform.right * input.normalized.x * currentSpeed)) * Time.deltaTime, Space.World); //move in direction facing, in world space
float animationSpeedPercent = ((running) ? 1 : .5f) * input.magnitude; //calculates the percent of movement speed
animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
//Debug.Log("Is grounded = " + isGrounded());
//Debug.Log("isJumping = " + isJumping);
}
bool isGrounded()
{
return Physics.CheckCapsule(coll.bounds.center, new Vector3(coll.bounds.center.x, coll.bounds.min.y - 0.1f, coll.bounds.center.z), coll.radius * 0.9f, groundLayers);
}
}
Your answer
Follow this Question
Related Questions
Should I have a player input class with events or input detection for everything that requires it? 0 Answers
Trouble with coroutine 0 Answers
unity3d player controlls not working sometimes 0 Answers
How do I stop an automatically repeating Coroutine? 1 Answer
Coroutine Not working properly. 1 Answer