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
2
Question by wijesijp · Aug 15, 2013 at 06:57 AM · tower-defense2dtoolkit

How to play correct animation based on direction?

I am making a tower defense game and I am using 2d toolkit to animate the creeps. I have 4 animations for left, right, up and down movement. I also have the direction vector of the creep.

How do I decide which animation to play using the direction vector?

Even if the creep is move at an angle I should still select one of these animations.
Looking for a simple method to decide on the correct animation.

Comment
Add comment · Show 1
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 mattssonon · Aug 15, 2013 at 01:55 PM 0
Share

Which animation do you want to be played if the creep moves at a 45 degree angle, e.g. towards (1, 1)?

1 Reply

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

Answer by 2d4Games · Aug 15, 2013 at 02:12 PM

Assuming the creep can only move in 4 directions (up, down, left right), you could make a series of if statements checking for the direction.

 if (directionVector.x == 1 && directionVector.y == 0){
     //direction is right
 } else if (directionVector.x == -1 && directionVector.y == 0){
     //direction is left
 } else if (directionVector.x == 0 && directionVector.y == 1){
     //direction is up
 } else if (directionVector.x == 0 && directionVector.y == -1){
     //direction is down
 }

Then, in each of the if statements, you can use a 2d toolkit object called tk2dSpriteAnimator. Save the 4 animations as tk2dSpirteAnimationClips, and then you can use

 tk2dSpriteAnimator.Play(tk2dSpriteAnimationClip clip);

to play the specific animation. So your final code segment should be something like:

 var animator : tk2dSpriteAnimator;
 var rightAnim : tk2dSpriteAnimationClip;
 var leftAnim : tk2dSpriteAnimationClip;
 var upAnim : tk2dSpriteAnimationClip;
 var downAnim : tk2dSpriteAnimationClip;
 
 if (directionVector.x == 1 && directionVector.y == 0){
         animator.Play(rightAnim);
     } else if (directionVector.x == -1 && directionVector.y == 0){
         animator.Play(leftAnim);
     } else if (directionVector.x == 0 && directionVector.y == 1){
         animator.Play(upAnim);
     } else if (directionVector.x == 0 && directionVector.y == -1){
         animator.Play(downAnim);
     }


EDIT: Just noticed that you also wanted them to move at an angle. In that case you check which direction that angle is closest to (like any angle between 45 degrees and 135 degrees would play an up animation). Again, this can be done with a variety of if statements.

 var movementAngle = Mathf.Atan(directionVector.y/directionVector.x);
 
 if (movementAngle >= Mathf.PI/4 && movementAngle < 3*(Mathf.PI/4)){
     //go up
 } else if (movementAngle >= 3*(Mathf.PI/4) && movementAngle < 5*(Mathf.PI/4)){
     //go left
 } else if (movementAngle >= 5*(Mathf.PI/4) && movementAngle < 7*(Mathf.PI/4)){
     //go down
 } else {
     //go right
 }
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 jfonz001 · Nov 08, 2017 at 10:22 PM 0
Share

I know this is super old, but thank you. I knew what I had to do but had no idea how to work out the math behind it.

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

LookAt Trigger for multiple objects 2 Answers

How to spawn enemy wave by wave? 2 Answers

Tower Defense Game Shooting Script Troubles 1 Answer

How to make a purchase option by dragging objects on phone? 0 Answers

Monodevelop - Code completion and 3rd party scripts 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