Search Unity

Collision detection and enums

Discussion in 'Scripting' started by Copywright, Jun 27, 2013.

  1. Copywright

    Copywright

    Joined:
    Apr 17, 2012
    Posts:
    21
    Hey,

    I'm having a problem. I have my animation set up with enumerated states and a switch statement in Update.

    Code (csharp):
    1.  public enum AnimationState
    2.     {
    3.         Idle,
    4.         Moving,
    5.         Hit,
    6.         Attacking,
    7.         Dying,
    8.         Dead
    9.     }
    10.     public AnimationState animState
    11.     {
    12.         get
    13.         {
    14.             return _animState;
    15.         }
    16.         set
    17.         {
    18.             // only update the state if it has changed
    19.             if (_animState != value)
    20.             {
    21.                 _animState = value;
    22.             }
    23.         }
    24.     }
    25. private void Update ()
    26.     {
    27.             switch (_animState)
    28.              {
    29.             case AnimationState.Idle:
    30.                 PlayAnimation("idle");
    31.                 anim.AnimationCompleted = null;
    32.                 velocity = Vector3.zero;
    33.  
    34.                 break;
    35.  
    36.             case AnimationState.Moving:
    37.                 PlayAnimation("walk");
    38.                 anim.AnimationCompleted = null;
    39.  
    40.                 break;
    41.  
    42.             case AnimationState.Hit:
    43.                 PlayAnimation("hit");
    44.                 velocity = Vector3.zero;
    45.                 anim.AnimationCompleted = HitCompleteDelegate;
    46.                
    47.                 break;
    48.  
    49.             case AnimationState.Attacking:
    50.  
    51.                 break;
    52.  
    53.             case AnimationState.Dying:
    54.  
    55.                 break;
    56.  
    57.             case AnimationState.Dead:
    58.  
    59.                 break;
    60.         }
    61.  
    62.  
    Then in a separate script I try to change the state through the public property in the other class:


    Code (csharp):
    1.  
    2.        private Enemy baseScript;
    3.  
    4.  
    5.     // Use this for initialization
    6.     void Start ()
    7.     {
    8.         baseScript = transform.parent.GetComponent<Enemy>();
    9.     }
    10.    
    11.     void OnTriggerEnter (Collider other)
    12.     {
    13.        if (other.gameObject.tag == "Player Attack")
    14.        {
    15.             baseScript.animState = Enemy.AnimationState.Hit;
    16.        }
    17.     }
    18.  
    Yet nothing happens when the player trigger collides with the enemy hitbox, besides from a debug.log that says "hit" which I put to make sure they're actually colliding.

    Is there some better way to do this? Also, would it be more practical to store enums outside classes? If so, how would you access them (I dont remember ever doing that).

    Thanks in advance.
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    i'm not sure but in each update you play the animation which means it starts from the beginning this could appear as nothing is animated. you should set a boolean in the property which indicates that a new animation has been assigned and only play an animation when this is true and set it to false immediately. so you only play the animation when it changes and when it finishes assign idle fe.

    if its "more practical" you must decide for yourself and how it matches your code organization structure. but its possible. i usually have a single cs file (no class!) called "Enumerations" and have all enums of the project there in alphabetical order. enums are like static classes accessible from anywhere (they are still nested in a namespace so you must include this when not standard namespace). you should only incorporate them inside a class when it is tied to that class and makes more sense to be nested.