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

Mecanim Control By Mouse Click

Discussion in 'Scripting' started by Paykoman, Dec 1, 2014.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys im trying to learn how to control a mecanim with mouse, like left key to walk and right key to standar attack like Diablo or Torchlight thing. The question is all the tutorials i found is to use WASD keys to move and space bar our any other key to attack. I try implement a mouse script but nothing work properly, i just want a idle state, run state and attack state, and dead :) nothing idle walk run , only idle and run... Can anyone help me or indicate me some place where i can find this plz? Appreciate
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    One solution is to use a NavMeshAgent and modify the ClickToMove script. Here's a version that works with a NavMeshAgent and Mecanim Animator with root motion. In the animator controller, add a float parameter named "Speed". Create a blend tree that blends on speed, where 0==idle and 1==walk/run. Remember to open the Navigation view and bake your NavMesh. The floor should be marked Static.

    Code (csharp):
    1. // Based on: http://wiki.unity3d.com/index.php/Click_To_Move_C by Vinicius Rezendrix
    2. using UnityEngine;
    3.  
    4. [RequireComponent(typeof(NavMeshAgent))]
    5. public class NavigateOnMouseClick : MonoBehaviour {
    6.  
    7.     public enum MouseButtonType { Left, Right, Middle };
    8.     public MouseButtonType mouseButton = MouseButtonType.Left;
    9.     public string speedParameter = "Speed";
    10.     public float distanceThreshold = 0.5f;
    11.  
    12.     private NavMeshAgent navMeshAgent;
    13.     private Animator animator;
    14.  
    15.      void Awake() {
    16.         animator = GetComponent<Animator>();
    17.         navMeshAgent = GetComponent<NavMeshAgent>();
    18.     }
    19.  
    20.     void Update() {
    21.         var speed = (navMeshAgent.remainingDistance < distanceThreshold) ? 0 : 1;
    22.         if (animator != null) animator.SetFloat("Speed", speed);
    23.  
    24.         // Moves the Player if the Mouse Button was clicked:
    25.         if (Input.GetMouseButtonDown((int) mouseButton) && GUIUtility.hotControl == 0) {
    26.             Plane playerPlane = new Plane(Vector3.up, transform.position);
    27.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    28.             float hitdist = 0.0f;
    29.             if (playerPlane.Raycast(ray, out hitdist)) {
    30.                 navMeshAgent.SetDestination(ray.GetPoint(hitdist));
    31.             }
    32.         }
    33.  
    34.         // Moves the player if the mouse button is held down:
    35.         else if (Input.GetMouseButton((int) mouseButton) && GUIUtility.hotControl == 0) {
    36.             Plane playerPlane = new Plane(Vector3.up, transform.position);
    37.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    38.             float hitdist = 0.0f;
    39.             if (playerPlane.Raycast(ray, out hitdist)) {
    40.             navMeshAgent.SetDestination(ray.GetPoint(hitdist));
    41.         }
    42.     }
    43. }
    And here's a version that uses Legacy animation:
    Code (csharp):
    1.  
    2. // Based on: http://wiki.unity3d.com/index.php/Click_To_Move_C by Vinicius Rezendrix
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(NavMeshAgent))]
    6. public class NavigateOnMouseClick : MonoBehaviour {
    7.  
    8.      public AnimationClip idle;
    9.     public AnimationClip run;
    10.  
    11.     public enum MouseButtonType { Left, Right, Middle };
    12.     public MouseButtonType mouseButton = MouseButtonType.Left;
    13.  
    14.     private Transform myTransform;
    15.     private NavMeshAgent navMeshAgent;
    16.     private Animation anim;
    17.  
    18.     void Awake() {
    19.         myTransform = transform;
    20.         anim = GetComponent<Animation>();
    21.         navMeshAgent = GetComponent<NavMeshAgent>();
    22.     }
    23.  
    24.     void Update() {
    25.         if (navMeshAgent.remainingDistance < 0.5f) {
    26.            if (idle != null && anim != null) anim.CrossFade(idle.name);
    27.         } else {
    28.           if (run != null && anim != null) anim.CrossFade(run.name);
    29.         }
    30.  
    31.         // Moves the Player if the Left Mouse Button was clicked:
    32.         if (Input.GetMouseButtonDown((int) mouseButton) && GUIUtility.hotControl == 0) {
    33.             Plane playerPlane = new Plane(Vector3.up, myTransform.position);
    34.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    35.             float hitdist = 0.0f;
    36.  
    37.              if (playerPlane.Raycast(ray, out hitdist)) {
    38.                 navMeshAgent.SetDestination(ray.GetPoint(hitdist));
    39.             }
    40.         }
    41.  
    42.         // Moves the player if the mouse button is held down:
    43.         else if (Input.GetMouseButton((int) mouseButton) && GUIUtility.hotControl == 0) {
    44.  
    45.             Plane playerPlane = new Plane(Vector3.up, myTransform.position);
    46.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    47.             float hitdist = 0.0f;
    48.  
    49.             if (playerPlane.Raycast(ray, out hitdist)) {
    50.                 navMeshAgent.SetDestination(ray.GetPoint(hitdist));
    51.             }
    52.         }
    53.     }
    54. }
    55.  
     
    Paykoman likes this.
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Wow.. Only u Tony :) ty very much. _O_
     
  4. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Tell me one thing.. in Animator i put idle as default state then i create new blend tree and connect with idle, or i simply create a blend tree and inside it i put run and idle animations blendin with Speed float?

     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    You only need one blend tree for idle + run. I call mine "Locomotion". When Speed is 0, all the blend weight is on the idle animation. When Speed is 1, all the blend weight is on the run animation. When Speed is 0.5, the weight is balanced, so it looks like a walk.
     
    Paykoman likes this.