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 KingAssassinable · Feb 12, 2018 at 06:17 PM · playercontrollercoroutinejumpcoroutine errors

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);
     }
 
 }
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

144 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 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 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 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

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


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