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
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.
// Based on: http://wiki.unity3d.com/index.php/Click_To_Move_C by Vinicius Rezendrix
using UnityEngine;
[RequireComponent(typeof(NavMeshAgent))]
public class NavigateOnMouseClick : MonoBehaviour {
public enum MouseButtonType { Left, Right, Middle };
public MouseButtonType mouseButton = MouseButtonType.Left;
public string speedParameter = "Speed";
public float distanceThreshold = 0.5f;
private NavMeshAgent navMeshAgent;
private Animator animator;
void Awake() {
animator = GetComponent<Animator>();
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update() {
var speed = (navMeshAgent.remainingDistance < distanceThreshold) ? 0 : 1;
if (animator != null) animator.SetFloat("Speed", speed);
// Moves the Player if the Mouse Button was clicked:
if (Input.GetMouseButtonDown((int) mouseButton) && GUIUtility.hotControl == 0) {
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
navMeshAgent.SetDestination(ray.GetPoint(hitdist));
}
}
// Moves the player if the mouse button is held down:
else if (Input.GetMouseButton((int) mouseButton) && GUIUtility.hotControl == 0) {
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
navMeshAgent.SetDestination(ray.GetPoint(hitdist));
}
}
}
And here’s a version that uses Legacy animation:
// Based on: http://wiki.unity3d.com/index.php/Click_To_Move_C by Vinicius Rezendrix
using UnityEngine;
[RequireComponent(typeof(NavMeshAgent))]
public class NavigateOnMouseClick : MonoBehaviour {
public AnimationClip idle;
public AnimationClip run;
public enum MouseButtonType { Left, Right, Middle };
public MouseButtonType mouseButton = MouseButtonType.Left;
private Transform myTransform;
private NavMeshAgent navMeshAgent;
private Animation anim;
void Awake() {
myTransform = transform;
anim = GetComponent<Animation>();
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update() {
if (navMeshAgent.remainingDistance < 0.5f) {
if (idle != null && anim != null) anim.CrossFade(idle.name);
} else {
if (run != null && anim != null) anim.CrossFade(run.name);
}
// Moves the Player if the Left Mouse Button was clicked:
if (Input.GetMouseButtonDown((int) mouseButton) && GUIUtility.hotControl == 0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
navMeshAgent.SetDestination(ray.GetPoint(hitdist));
}
}
// Moves the player if the mouse button is held down:
else if (Input.GetMouseButton((int) mouseButton) && GUIUtility.hotControl == 0) {
Plane playerPlane = new Plane(Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast(ray, out hitdist)) {
navMeshAgent.SetDestination(ray.GetPoint(hitdist));
}
}
}
}
Wow… Only u Tony ty very much. O
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?
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.