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 oliver-jones · May 14, 2013 at 12:27 AM · directiontarget

Change Animation According to Direction

Hello,

I'm working on a RTS game,

I have a character that has 5 animations attached: Idle, Run, RunLeft, RunRight, RunBack. What I need to do, is use these animations for my character depending on the direction he's running.

By default, the character will always run in a forward direction to wherever I click, so I only need to use the Run animation.

But, when the character comes into close proximity with an enemy, his rotation will always face the enemy, but will continue to run in the direction of wherever I clicked on the map.

This is where I need the change the animation playing, as he may be facing North at the enemy, but running East, so I will need to play the RunRight animation instead of the Run (forward) animation.

I need to somehow use the current character rotation against the marker (the click location of the mouse, on the map) so select between my animations:

 //If we are in proximity to an enemy, then face it, but continue to run towards click marker
 if(finder.target){
     transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(finder.target.position - transform.position), turnSpeed * Time.deltaTime);
     transform.position = Vector3.MoveTowards(transform.position, nav.CurrentTargetWaypoint, runSpeed * Time.deltaTime);
 }
 //Otherwise, face the the click marker, and run towards
 else{
     transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(nav.CurrentTargetWaypoint - transform.position), turnSpeed * Time.deltaTime);
     transform.position += transform.forward * runSpeed * Time.deltaTime;
 }
 
 transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
 
 //Animation control
 if(hasAnimation){
     anim.characterToAnimate.animation.CrossFade(anim.Run.name, .1f);
 }
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
Best Answer

Answer by oliver-jones · May 14, 2013 at 12:45 AM

Okay,

So I've been messing and tweaking, and I've seemed to have done it, although I'm not sure if this is the best way to do it:

 //Animation control
 if(hasAnimation){
 
     if(finder.target){
         
         var angleBetween = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(nav.CurrentTargetWaypoint - transform.position));
 
         if(angleBetween > 0 && angleBetween < 90)
             anim.characterToAnimate.animation.CrossFade(anim.RunRight.name, .1f);
 
         else if(angleBetween > 270 && angleBetween < 360)
             anim.characterToAnimate.animation.CrossFade(anim.RunLeft.name, .1f);
 
         else if(angleBetween > 90 && angleBetween < 270)
             anim.characterToAnimate.animation.CrossFade(anim.RunBack.name, .1f);
 
         else
             anim.characterToAnimate.animation.CrossFade(anim.Run.name, .1f);
     }
     else
         anim.characterToAnimate.animation.CrossFade(anim.Run.name, .1f);
 
 }

I'm basically just changing the animation based on every 90 degree angle the character is at compared to the nav marker.

Hope this helps someone.

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

14 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

Related Questions

enemy targetting not working 0 Answers

Angle to Direction 1 Answer

throw object at specific direction or angle 2 Answers

Move GameObject In A Direction 1 Answer

Rotate a GameObject [set front] 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