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 loizos-95b · Nov 30, 2017 at 08:29 PM · directionwaypointpatrolchasefinite-state-machine

How to change enemy's Direction on FiniteStateMachine???

Hello coders.. just a quick question to make: I have a small terrain with a character that is moving on it(user input- keyboard&mouse), and i also have a enemy object that has a Finite state machine. However, I think the State Machine is working fine from IDLE to PATROL and back.

     public enum State { IDLE, PATROL, CHASE} //ALL STATES IN ENUM
     public State myState; //ONE STATE
     private bool alive; 
 
     public GameObject[] wayPoints;  
     private int wayPointIndex = 0; //Store the current index of waypoints
 
     void Start()
     {
         StartCoroutine("FiniteStateMachine"); //Call State Machine
     }
 
     IEnumerator FiniteStateMachine()
     {
         //While the tiger is alive do switch statement for the State Machine
         while (alive)
         {
             switch (myState)
             {
                 case State.IDLE:
                     Idle(); //Tiger Is not Moving
                     break;
                 case State.PATROL:
                     Patrol(); //Tiger Is Moving from Waypoint to Waypoint
                     break;
                 case State.CHASE:
                     Chase(); //Tiger Is Chasing the Character
                     break;
             }
             yield return null;
         } //End of While Loop

How should i change the Enemy's direction when the States are changing since it has a different direction for wayPoints and different direction for the target using Vector 3 for both cases.

  private Vector3 wayPointDirection; //Use for the Way Points (PATROL State)
  private Vector3 targetDirection; //Use for the target (CHASE State)

If you have a question ill be happy to answer asap. and i will really appreciate it if you guys will help me out fix this issue. :))

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
1

Answer by Buckslice · Nov 30, 2017 at 08:37 PM

I'm not sure how your tiger's movement is setup but if you have something like a current direction variable you can just set currentDir = wayPointDir every frame in the Patrol() function and currentDir = targetDirection every frame in the Chase() function. Then use that variable in your movement code.

If you want to smoothly change direction and not have it snap instantly upon state switch you could use something like this

 // patrol example
 currentDir = Vector3.Lerp(currentDir, wayPointDir, Time.deltaTime * 2.0f);
Comment
Add comment · Show 2 · 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 loizos-95b · Nov 30, 2017 at 10:00 PM 0
Share

Hello Buckslice, thanks for your feedback its working great. Let me know if you recommend a more efficient way of doing it.

 //$$anonymous$$ove to Target
 targetDirection = target.transform.position - transform.position;
     
 currentDirection = targetDirection; //Set current direction to the target 
     
 //Rotate Target
 this.transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(currentDirection), tigerWalkSpeed * Time.deltaTime);
 
 this.transform.Translate(0, 0, tigerRunSpeed * Time.deltaTime);


avatar image Buckslice loizos-95b · Nov 30, 2017 at 10:45 PM 0
Share

Looks good, glad I could 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

73 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Have Ghost go from Patrolling to Chasing - Pacman 0 Answers

AI faces Weird Direction when following a waypoint 0 Answers

NPC won't move after adding chase code. 1 Answer

City traffic Lanes 3 Answers

Patrol routes and returning to them? 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