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 /
avatar image
0
Question by ragefordragons · Apr 15, 2018 at 04:08 PM · animationanimatorplayer movementloopingtransitions

Having some animation trouble

So I have created a run animation and a jump animation and an idle animation that can all transition bewteen eachother. However, the jump animtion is giving me trouble. Whenever the animation is triggered, it plays several times and then reverts to the idle after a few loops. First of all, I don't even know hows thats possible becase it is only supposed to revert to idle once you have been grounded again, so thats pretty weird. Aslo I dont want it to loop, just run once and then hold on the end frame until the animation is complete. Is there nayway to fix this?

Here is my code: (sorry this is my entire player controller, I mean you never know what might be relevant) using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Sprites;

public class playerController : MonoBehaviour {

 //MOVEMENT
 [Tooltip("The maximum horizontal movement speed in units per second.")]
 public float speed = 8; // movenment speed for Luca

 [Tooltip("The maximum vertical jump speed in units per second.")]
 public float jumpspd = 12; // jumo height for Luca

 private float moveInput;
 private Rigidbody2D rb;

 
 public Transform groundCheck;
 public float checkRadius;
 public LayerMask whatIsGround;

 public static bool canVBeset;

 //ANIMATION
 public Animator anim;
 bool canDash = true;
 public bool grounded;
 bool isRunning;
 public SpriteRenderer ren;
   

 //UNUSED AS OF NOW
 public static float LucaHP;
 public static float LucaSP;
   

 //TODO: MOVE ONTO NEW SCRIPT
   




 // Use this for initialization
 void Start()
 {

     canVBeset = true;

     anim = gameObject.GetComponent<Animator>();
     anim.SetBool("isRunning", false);
     anim.SetBool("dashing", false);
     anim.SetBool("isJumping", false);
    

     canDash = true;

     rb = GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame

 void Update()
 {
     HandleDash();
     HandleFlip();
     HandleJump();
     anim.SetBool("isRunning", isRunning);

     if (Input.GetButton("Horizontal"))
     {
         isRunning = true;

     }
     else
     {
         isRunning = false;
     }
     

     if (grounded == true)
     {
         anim.SetBool("isJumping", false);
     }
     
 }

 void FixedUpdate()
 {
     if (canDash == true && canVBeset == true)
     {
         playermove();
         
     }

 }






 void playermove()
 {
     grounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);


    
     float axixX = Input.GetAxis("Horizontal");
     //transform.Translate(new Vector2(axixX, 0) * Time.deltaTime * speed);
     

     rb.velocity = new Vector2(axixX * speed * Time.deltaTime, rb.velocity.y);
     

     
 }

 void HandleJump()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         if (grounded == true)
         {
             GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpspd);
             anim.SetBool("isJumping", true);
             grounded = false;

         }
     }

    
 }

 void HandleDash()
 {
     if (Input.GetKeyDown(KeyCode.LeftShift))
     {
         StartCoroutine(dash());
     }
 }

 void HandleFlip()
 {
     if (Input.GetKeyDown(KeyCode.A))
     {
         ren.flipX = true;
     }

     if (Input.GetKeyDown(KeyCode.D))
     {
         ren.flipX = false;
     }
 }

 IEnumerator dash()
 {
     if (canDash == true)
     {
         
         canDash = false;
         anim.SetBool("dashing", true);
         if (ren.flipX == true)
     {
         GetComponent<Rigidbody2D>().velocity = new Vector2(-jumpspd , GetComponent<Rigidbody2D>().velocity.y);

     }
     else
     {

             GetComponent<Rigidbody2D>().velocity = new Vector2(jumpspd , GetComponent<Rigidbody2D>().velocity.y);

     }
     yield return new WaitForSeconds(0.2f);
     
         anim.SetBool("dashing", false);
         canDash = true;
     }
 }

 

 
   

}

Comment
Add comment · Show 5
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
avatar image shadowpuppet · Apr 15, 2018 at 09:42 PM 0
Share

in Update you are saying if (grounded == true) anim.SetBool ("isJumping", false)

and also saying in HandleJump that if (grounded == true) anim.SetBool("isJumping", true) and in your Update you are calling HandleJump everyframe?

I don't know if that is the problem but it has me confused. I mean after the jump you are setting grounded = false, so in the update function isJumping doesn't become false because grounded is false?

avatar image ragefordragons shadowpuppet · Apr 15, 2018 at 11:18 PM 0
Share

Yeah your right, I never noticed how those were kind of contradicting each other. I have an idea using a courotine that might fix it but do you have a different idea? Thanks regardless for pointing that out.

avatar image shadowpuppet ragefordragons · Apr 15, 2018 at 11:33 PM 0
Share

no better idea here, that is what I was thinking as well. a coroutine to set isJumping to false again. or....since the animation is already triggered on $$anonymous$$eyDown...maybe try keyUp to set grounded to true or isJumping false?

Show more comments
avatar image ragefordragons shadowpuppet · Apr 15, 2018 at 11:32 PM 0
Share

Yeah okay that did not work.... Ill put this on reddit for more guidance.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Rugbug_Redfern · Apr 15, 2018 at 11:43 PM

Go into the Project tab in unity and click on the animation file. Uncheck "Loop Mode." This will stop the animation from looping. I don't think the problem is with your code, try checking the Animator. Make sure "exit time" is set to false (unchecked) on the transitions.

Comment
Add comment · Show 1 · Share
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
avatar image ragefordragons · Apr 16, 2018 at 01:38 AM 0
Share

Yeah so looping is unchecked and none of the transitions have exit time. So that changed it, but didnt fix it. Now it loops once, but after its done it imediately reverts back to idle, which it shouldnt becase of the booleans I have set up, this is why I think its my code.

avatar image
0

Answer by shadowpuppet · Apr 16, 2018 at 02:19 PM

Here's an idea. Instead of having HandleJump(); in the Update function put the keydown there. I don't use bools i use SetTrigger and this works for me because as the code runs from top down and sets the trigger to start the animation, resetting the trigger doesn't affect the running of the animation once started. Maybe that is true for setting bools to true as well? so once the conditions are met to start the animation I revert the conditions so they don't repeat the animation. But not sure if setting the two conditions to false will mess up the Vector 2 code - like if it will stop running once the conditions reversed. So...maybe back to using a coroutine to do that

  void Update(){
  if (Input.GetKeyDown(KeyCode.Space))
 grounded = false;
  anim.SetBool("isJumping", true);
     HandleJump();
 }
     }
 
      void HandleJump()
      {
              if (grounded == false)
              {
                  GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpspd);
                  grounded = true;
   anim.SetBool("isJumping", false);
              }
          }
         
      }




  
 



Comment
Add comment · Share
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

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

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

How can I make my guy stop running? 1 Answer

Continuously Play Animation Forwards and Backwards with Mechanim 0 Answers

Animator won't let me add transitions 1 Answer

Can't move player in Bomberman after applying simple animation 0 Answers

How can I make a state pass after a reversed state? 0 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