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 /
This question was closed Jul 10, 2017 at 06:59 PM by boxmyth for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by boxmyth · Jul 10, 2017 at 05:21 PM · state-machineif-else

State Machine If Else Distance question (c#)

Hi all. Having a bit of an issue with a state machine script. I need to determine the best way to differentiate between two similar distances for use in If/Else. Default state happens when > warningDistance (10f), but I also need another similar distance, safeDistance (11f) to be used for another state later on in the script, but I’m not sure how to make it unique. I tried adding a true/false bool with &&, but that didn’t work. safeDistance and warningDistance are very close to each other, but the script only seems to recognize warningDisance for First Action and never uses safeDistance to make it to the Fourth Action. Any ideas?

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class StateDistanceTesting : MonoBehaviour {

 public float safeDistance = 11f; //Not Close
 public float warningDistance = 10f; //Kinda Close
 public float dangerDistance = 2f; //Really Close
 private bool checkDistance = true;

 public GameObject bombObject; //Bomb
 public GameObject playerObject; //Player

 private Transform target;
 private Animator anim;

 public enum POActionType
 {
     FirstAction,
     SecondAction,
     ThirdAction,
     FourthAction
 }

 private POActionType eCurState = POActionType.FirstAction;

 void Start () 
 {
     anim = GetComponent<Animator>();
     target = playerObject.transform;
 }

 void Update () 
 {
     switch (eCurState)
     {
     case POActionType.FirstAction:
         HandleFirstAction();
         break;

     case POActionType.SecondAction:
         HandleSecondAction();
         break;

     case POActionType.ThirdAction:
         HandleThirdAction();
         break;

     case POActionType.ThirdAction:
         HandleFourthAction();
         break;
     }
 }

 void HandleFirstAction()
 {
     if (checkDistance) 
     {
         var distance = Vector3.Distance (bombObject.transform.position, transform.position);
         if (distance > warningDistance) 
         {
             anim.Play("1STACTION");
             Debug.LogWarning("First Action");
         }

         else if (distance < warningDistance) 
         {
             Debug.LogWarning ("Handle Second Action");
             HandleSecondAction ();
         }
     }
 }

 void HandleSecondAction()
 {
     if (checkDistance) 
     {
         var distance = Vector3.Distance (bombObject.transform.position, transform.position);
         if ((distance <= warningDistance) && (distance > dangerDistance)) 
         {
             anim.Play ("2NDACTION");
             eCurState = POActionType.SecondAction; //ANSWER THANKS TO ahorne: THIS LINE WAS MISSING
             Debug.LogWarning ("Second Action");
         } 
         else if (distance < dangerDistance) 
         {
             Debug.LogWarning ("Handle Third Action");
             HandleThirdAction ();
         }
         else if (distance > safeDistance) //this isn't getting called
         {
             Debug.LogWarning ("Handle Fourth Action");
             HandleFourthAction ();
         }
         else if ((distance < safeDistance) && (distance > warningDistance))
         {
             Debug.LogWarning ("Handle Fourth Action");
             HandleFourthAction ();
         }
     }
 }

 void HandleThirdAction()
 {
     anim.Play("3RDACTION");
     eCurState = POActionType.ThirdAction; //ANSWER THANKS TO ahorne: THIS LINE WAS MISSING
     Debug.LogWarning("Third Action");
 }

 void HandleFourthAction()
 {
     anim.Play("4THACTION");
     eCurState = POActionType.FourthAction; //ANSWER THANKS TO ahorne: THIS LINE WAS MISSING
     Debug.LogWarning("Fourth Action");
 }

}

Comment
Add comment · Show 2
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 Jinkata · Jul 10, 2017 at 06:18 PM 0
Share

It's never getting called or it just doesn't get called if your distance is between 10 and 11? From what I can see the logic looks good except situations where your distance in between 10 and 11 because there is no condition to handle that.

avatar image boxmyth Jinkata · Jul 10, 2017 at 06:34 PM 0
Share

I tried adding an else if ((distance < safeDistance) && (distance > warningDistance)) to cover that distance gap but it still ignores the Fourth Action and returns to the First Action.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by ahorne · Jul 10, 2017 at 06:39 PM

Don't forget to change eCurState as your state changes. With your current code, every time through Update it will only ever use HandleFirstAction();

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 boxmyth · Jul 10, 2017 at 06:48 PM 0
Share

Of course! Thanks ahorne! That worked!

avatar image ahorne boxmyth · Jul 10, 2017 at 06:57 PM -1
Share

ur welcome. glad to help.

Follow this Question

Answers Answers and Comments

69 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

Related Questions

Segment within a nested If does not get evaluated 2 Answers

CoRoutine without MonoBehavior possible? 1 Answer

Porting C++ game, State machines and UI 0 Answers

rpg turn based attack enemy is not attacking 1 Answer

How to delete a StateMachineBehaviour via Editor Script? 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