Search Unity

Animating an enemy and changing states

Discussion in 'Scripting' started by GamesOnAcid, Feb 5, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy : MonoBehaviour {
    5. public Transform thisObject;
    6. public Transform target;
    7. private NavMeshAgent navComponent;
    8. Animator anim;
    9. int runningStateHash = Animator.StringToHash("Base Layer.Running");
    10. int walkingStateHash = Animator.StringToHash("Base Layer.Walking");
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         target = GameObject.FindGameObjectWithTag("Player").transform;
    16.         navComponent = this.gameObject.GetComponent<NavMeshAgent>();
    17.         anim = GetComponent<Animator> ();
    18.     }
    19.  
    20.     void OnTriggerEnter(Collider col)
    21.     {
    22.         AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo (0);
    23.     }
    24.  
    25.     void OnTriggerExit(Collider col)
    26.     {
    27.         AnimatorStateInfo stateInfo = anim.GetCurrentAnimatorStateInfo (1);
    28.     }
    29.        
    30.     void Update()
    31.     {
    32.         float dist = Vector3.Distance(target.position, transform.position);
    33.         float move = Input.GetAxis ("Vertical");
    34.         anim.SetFloat ("Speed", move);
    35.    
    36.  
    37.         if(target)
    38.         {
    39.             navComponent.SetDestination(target.position);
    40.         }
    41.  
    42.         else
    43.         {
    44.             if(target = null)
    45.             {
    46.                 target = this.gameObject.GetComponent<Transform>();
    47.             }
    48.             else
    49.             {
    50.                 target = GameObject.FindGameObjectWithTag("Player").transform;
    51.             }
    52.         }
    53.     }
    54. }
    I'm using the following code to call animations with the OnTriggerEnter and Exit functions, and I want to call seperate animations depending on if you are in the trigger or not. How can I call seperate animations other than how I am doing it?
     
  2. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
  3. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    Ok, so using
    Code (CSharp):
    1.     void OnTriggerStay(Collider col)
    2.     {
    3.         if (col.attachedRigidbody)
    4.         {
    5.  
    6.         }
    7.     }
    8.  
    How would I call each animation? I set them as variables(or integers) with
    Code (CSharp):
    1. int runningStateHash = Animator.StringToHash("Base Layer.Running");
    2. int walkingStateHash = Animator.StringToHash("Base Layer.Walking");
    3.  
    How would I call each individual one?
     
  4. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    I usually do something like this to toggle a parameter in the animator, your layers in the Animator should then know what to do if the animator is setup correctly.

    Code (CSharp):
    1.     void OnTriggerStay(Collider other){
    2.         if(other.attachedRigidbody){
    3.             GetComponent<Animator>().SetBool("Running", true);
    4.         }
    5.     }
    6.  
    7.     void OnTriggerExit(Collider other){
    8.         if(other.attachedRigidbody){
    9.             GetComponent<Animator>().SetBool("Running", false);
    10.         }
    11.     }
    12.  
     
  5. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    283
    I set everything up, and he now walks when I am outside the trigger, but when I enter the trigger, he only sprints for a few seconds, then walks, then sprints again. I think it might be because I have a transition between the two? Should I only have 1 transition?