Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Peakz · Mar 28, 2017 at 07:23 AM · animation2dprogramming2d animationanimating

Idle Animation Not Playing

Hi all, I am currently testing 2D game development. After finally getting my follower script to work and have a sprite follow me, I am attempting to animate this follower too. I'm sure you can already tell I'm new to Unity and games development in general so please point out anything wrong!

My problem is that my animations are playing for my actual movements (walking up, down, left and right), but whenever I stop my idle animations are not playing even though my isMoving boolean is false, and the moving animations continue to play even when the follower is not moving. Here is my code:

     public Transform target;
     public float speed;
     public float stopDistance = 1f;
     Animator anim;
     Rigidbody2D rbody;
 
     void Start()
     {
         anim = GetComponent<Animator>();
         rbody = GetComponent<Rigidbody2D>();
         anim.speed = 0.65f;
     }
     
 
     void Update()
     {
         //follow player
         var distance = Vector2.Distance(transform.position, target.position);
         float step = speed * Time.deltaTime;
 
         if (distance > stopDistance)
         {
             transform.position = Vector2.MoveTowards(transform.position, target.position, step);
         }
 
 
         //getting direction of movement and therefore animating it
         var heading = target.position - transform.position;
         var moveDistance = heading.magnitude;
         Vector2 moveDirection;
         // Get the direction of the player
         moveDirection = heading / moveDistance;
         // Make this vector planar (xz-plane)
 
         if (moveDirection != Vector2.zero)
         {
             anim.SetBool("isMoving", true);
             anim.SetFloat("moving_x", moveDirection.x);
             anim.SetFloat("moving_y", moveDirection.y);
         }
         else
         {
             anim.SetBool("isMoving", false);
         }
     }


From testing around with the code I think the problem is to do with the var heading = target.position - transform.position;, because with my stopping distance preventing the follower getting too close, it will always leave a gap distance between the follower and player, and I feel that this gap is always setting the value of 'heading' to be > 0.

I think this is the problem because when I remove the stopping distance all together and disable the sprite renderer, I can see that the follower does not move (/is idle when it is directly on top of the player). As well as the 'moving_x' and 'moving_y' parameters being always above/below 0 and never actually 0 in the animation pane while moving. As stated before I feel like this is caused by the gap inbetween the follower and player caused by the stopping distance, however I cannot work out any fix for this. So any solution would be greatly appreciated!

Comment
Add comment · Show 3
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 rob5300 · Mar 28, 2017 at 10:39 AM 0
Share

If i understood correctly the issue, why don't you just check if the heading is less than say 0.1, set heading to 0?

avatar image Peakz rob5300 · Mar 28, 2017 at 02:49 PM 0
Share

Thanks for your reply. That's a good idea but when I tried:

   if (heading < 0.1)
         {
             heading = 0;
         }


It gave me the error 'Operator '>' cannot be applied to operands of type 'Vector2' and 'double''. Have I missed a step here?

avatar image Brian-FJ · Mar 28, 2017 at 04:25 PM 1
Share

Well the answer to this question is that heading is a Vector2. That means there are two values in it, x and y.

You either want to check the direction that you move (probably x) or else the magnitude of x and y if you care about both directions.

  if ($$anonymous$$athf.abs(heading.x) < 0.1)
              heading = Vector2.zero;
 
 // or
 
  if (heading.magnitude < 0.1)
              heading = Vector2.zero;

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Brian-FJ · Mar 28, 2017 at 04:30 PM

Hi Peakz. I think the main thing you're doing wrong is checking if moveDirection is zero. As you said that distance won't ever really be zero.

However you already know everything you need. The line near the start that goes "if (distance > stopDistance)" looks like it does the check you need. What about some code like this?

 if (distance > stopDistance)
          {
              anim.SetBool("isMoving", true);
              anim.SetFloat("moving_x", moveDirection.x);
              anim.SetFloat("moving_y", moveDirection.y);
          }
          else
          {
              anim.SetBool("isMoving", false);
          }
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 Peakz · Mar 28, 2017 at 05:08 PM 0
Share

Just tested this and it works! Thanks for the help!!!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Sprite Rect Moves Between Frames 0 Answers

Making interchangable 2d body parts for 2d / 3d hybrid game, possible? 0 Answers

How can I do multiple animations on my character such as Start jump, jump and a landing after the jump 2D 0 Answers

2D Animation does not start 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