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 /
  • Help Room /
avatar image
0
Question by Spider_newgent · Jun 15, 2016 at 04:35 PM · triggeranimator controllerdirectionstate-machinedistance check

change animation to make character face a waypoint.

Hi.

I'm making a point and click game in 2D. My player is a sprite and has a different animation for walking north, east, south and west. The player checks each Update to see if there is a waypoint in the queue. If there is, he walks toward it. On reaching the destination the waypoint is deleted and he starts moving to the next one.

I'm struggling with determining which direction he is facing, so that I can change his animation. My initial thoughts were to determine the distance to the next waypoint on the X & Y. If X is greater than Y the character should walk horizontally, and if X > 0 he's moving east, X < 0 would be west etc.

The code I have does change the animation, but the player stutters and moonwalks like the ghost of Michael Jackson, so my logic is clearly incorrect.

This is the snippet I have so far:

 case State.Walking:
                 Debug.Log(playerState);
 
                 //**DETERMINE DIRECTION OF SPRITESHEET, (N,E,S,W)
                 float distX = transform.position.x - wayPoints[0].x;
                 float distY = transform.position.y - wayPoints[0].y;
 
                 //East
                 if (distX > distY && distX >= 0)
                 {
                     animator.SetTrigger("walkEastWest");
                     sr.flipX = true;
                 }
 
                 //West
                 if (distX > distY && distX < 0)
                 {
                     animator.SetTrigger("walkEastWest");
                     sr.flipX = false;
                 }
 
                 //North
                 if (distX < distY && distY >= 0)
                 {
                     animator.SetTrigger("walkUp");
                 }
 
                 //South
                 if (distX < distY && distY < 0)
                 {
                     animator.SetTrigger("walkDown");
                 }
 
                 //**ANIMATE THE POSITION, SET AND REMOVE WAYPOINTS ETC
                 if (transform.position != destination) {
                     transform.position = Vector3.MoveTowards(transform.position, destination, step);
                 }
 
                 if (transform.position == destination) {
                     wayPoints.RemoveAt(0);
 
                     if (wayPoints.Count > 0){
                         destination = wayPoints[0];
                         //measure distance/direction
                     }
 
                     if (wayPoints.Count <= 0) {
                         playerState = State.Idle;
                     }
                 }
                 r.material.color = Color.red;
                 break;

I'm happy to post any further code/info needed to sort this out.

Thanks

Dan

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

0 Replies

· Add your reply
  • Sort: 

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

68 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

Related Questions

How to implement gun animations 0 Answers

Animation which uses trigger is played twice before going back to the previous state. 0 Answers

Why StateMachineBehaviour not calling OnStateExit when doing animator.Crossfade through code? 0 Answers

How do I start/stop the movement of transform.Translate() if a certain animation is playing in the animator? 0 Answers

How to replace an Animation State in Animator, without rebuilding all the transitions? 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