Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Animator issue

Discussion in 'Scripting' started by Paykoman, Sep 10, 2015.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys im trying to implement animator in my click to move game but right now im haveing an issue, the idle works fine but when i try run the character keep idle and only sometimes turn to running.

    I hv my animator like the image bellow with 2 parameters, so i change idle to run when IsRunning is true and vice-versa:
    - bool IsRunning
    - trigger Die

    And i hv this script attached to player, can anyone help plz???



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6.    
    7. public class ClickToMove : MonoBehaviour
    8. {
    9.    
    10.  
    11.     NavMeshAgent navAgent;
    12.  
    13.  
    14.     public float attackDistance = 10f;
    15.  
    16.     public float attackRate = 0.5f;
    17.  
    18.     private Animator anim;
    19.  
    20.     private Transform targetedEnemy;
    21.  
    22.     private Ray shootRay;
    23.  
    24.     private RaycastHit shootHit;
    25.  
    26.     private bool running;
    27.  
    28.     private bool enemyClicked;
    29.  
    30.     private float nextHit;
    31.  
    32.     // Use this for initialization
    33.     void Awake ()
    34.     {
    35.         anim = GetComponent<Animator>();
    36.  
    37.         navAgent = GetComponent<NavMeshAgent>();
    38.     }
    39.  
    40.    
    41.     void Update()
    42.     {
    43.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    44.         RaycastHit hit;
    45.         if (Input.GetMouseButtonDown(0))
    46.         {
    47.             if(Physics.Raycast(ray, out hit, 100))
    48.             {
    49.                 if(hit.collider.CompareTag("Enemy"))
    50.                 {
    51.                     targetedEnemy = hit.transform;
    52.                     enemyClicked = true;
    53.                 }
    54.                 else
    55.                 {
    56.                     running = true;
    57.                     enemyClicked = false;
    58.                     navAgent.destination = hit.point;
    59.                     navAgent.Resume();
    60.                 }
    61.             }
    62.         }
    63.  
    64.         if (enemyClicked)
    65.         {
    66.             MoveAndHit();
    67.         }
    68.  
    69.         if (navAgent.remainingDistance <= navAgent.stoppingDistance)
    70.         {
    71.             running = false;
    72.         }
    73.         else
    74.         {
    75.             running = true;
    76.         }
    77.  
    78.         anim.SetBool ("IsRunning", running);
    79.     }
    80.  
    81.     private void MoveAndHit()
    82.     {
    83.         if (targetedEnemy == null)
    84.             return;
    85.          navAgent.destination = targetedEnemy.position;
    86.         if (navAgent.remainingDistance >= attackDistance)
    87.         {
    88.             navAgent.Resume();
    89.             running = true;
    90.         }
    91.  
    92.         if (navAgent.remainingDistance <= attackDistance)
    93.         {
    94.             transform.LookAt(targetedEnemy);
    95.             Vector3 dirToHit = targetedEnemy.transform.position - transform.position;
    96.  
    97.             if(Time.time > nextHit)
    98.             {
    99.                 nextHit = Time.time + attackRate;
    100.                 //attackmethod
    101.             }
    102.             //navAgent.Stop();
    103.             //running = false;
    104.         }
    105.     }
    106. }
    107.  
     
  2. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,121
    Remove the exit time from the transition.
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    It works but now i hv a problem after idle animation run 1st time, then its not looping if i stay stopped... All the rest works fine now.
     
  4. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,121
    Not looping? The animation? Make sure you make the animation clip looped. Select the clip in your project and set it to loop.
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I check now that i get a warning at line 78 saying Animator has not been initialized...
     
  6. scifresh

    scifresh

    Joined:
    Oct 8, 2013
    Posts:
    23
    can you also post your attack method ?
     
  7. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    It works ty m8
     
  8. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    i dont hv attack method yet loool... im trying figure it out xD im kind new in this things
     
  9. scifresh

    scifresh

    Joined:
    Oct 8, 2013
    Posts:
    23
    instead of using bools to check the current action, use state machine. its basically an enum and a switch case in update method.
     
  10. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I just follow some tutorial for this one cuz im not very familiar with state machines and animations... :( and if will have lots of problems when i implement combat for sure lol