Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jimbohutchings · Dec 02, 2013 at 06:50 PM · animationbugrepeat

Why does my animation repeat? (CSharp)

 using UnityEngine;
 using System.Collections;
 
 public class Jump : MonoBehaviour 
 {
     public string jumpButton = "Fire1";
     public float jumpPower = 10.0f;
     public Animator anim;
     public bool grounded = false;
     public float minJumpDelay = 0.5f;
     public Transform groundCheck;
     private float jumpTime = 0.0f;
     private bool jumped = false;
 
     // Use this for initialization
     void Start ()
     {
         anim = gameObject.GetComponent<Animator>();
     }
     
     // Update is called once per frame
     void Update () 
     {
         grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
         jumpTime -= Time.deltaTime;
         if(Input.GetButtonDown(jumpButton) && grounded)
         {
             jumped = true;
             grounded = false;
             anim.SetTrigger("Jump");
             rigidbody2D.AddForce(transform.up*jumpPower);
             jumpTime = minJumpDelay;
         }
         if(grounded && jumpTime <= 0 && jumped)
         {
             jumped = false;
             anim.SetTrigger("Land");
         }
 
         if(grounded == false && jumped == false)
         {
             anim.SetTrigger("Fall");
         }
         if(grounded)
         {
             anim.SetTrigger("Fall_Land");
         }
 
         
     }
 }

This section here controls when to play my falling animation on my 2D game (unity 4.3) however when my character is falling its all ok but when a land or fall from a ledge it repeats the transition to the fall twice. So it goes from walking animation to falling to walking to falling in the space of a few frames.

 if(grounded == false && jumped == false)
         {
             anim.SetTrigger("Fall");
         }
         if(grounded)
         {
             anim.SetTrigger("Fall_Land");
         }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jabez · Dec 02, 2013 at 08:28 PM

Where's grounded == true? by the looks of it, it's always set to false. jump is also always set to false unless you jump, so it will always call this line

 if(grounded == false && jumped == false)
 {
 anim.SetTrigger("Fall");


if I'm wrong don't judge me, I'm fairly new to C#.

Comment
Add comment · Show 4 · 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 The Kracken · Dec 02, 2013 at 08:46 PM 0
Share
 grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << Layer$$anonymous$$ask.NameToLayer("Ground"));

That is where grounded == true, Assu$$anonymous$$g the linecast returns true.

avatar image jabez · Dec 02, 2013 at 09:06 PM 0
Share

Thanks for the info! c:, as i said im fairly new. he could try this i wrote for him, probably wont work though, http://pastebin.com/BTb20Tte he has to add a few more if statements but other then that, yeah.

avatar image jimbohutchings · Dec 04, 2013 at 07:00 PM 0
Share

Thanks for all your answers this is my first forum post! I am just trying your script out now.

avatar image jimbohutchings · Dec 04, 2013 at 07:04 PM 0
Share

Hey thanks for the script however it wont seem to work. The problem is when I hit the ground it changes to the right animation then back to the falling then back to the right animation again in a space of two frames.

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

18 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

Related Questions

Multiple Cars not working 1 Answer

i get weird rotations on my animated objects using legacy, help? 0 Answers

My character keep walking left. (bug) 0 Answers

How to get gun to move with models hand? 1 Answer

Problem with Hash IDs & End of animation detection 2 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