1.简单的位移,以及动画播放
using UnityEngine;
using System.Collections;
public class ClickToMove : MonoBehaviour {
public float speed;
public CharacterController controller;
public AnimationClip idle;
public AnimationClip run;
private Vector3 position;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton(1))
{
locatePosition();
}
moveToPosition();
}
void locatePosition()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000))
{
position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
Debug.Log(position);
}
}
void moveToPosition()
{
if (Vector3.Distance(transform.position, position) > 0.4)
{
Quaternion newRotation = Quaternion.LookRotation(position - transform.position, Vector3.forward);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
controller.SimpleMove(transform.forward * speed);
animation.CrossFade(run.name);
}
else
{
animation.CrossFade(idle.name);
}
}
}