using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
private Transform player;
public float moveSpeed;
public float rotateSpeed;
public Transform jianTou;
public bool showJianTou = false;
public bool isKey = true; // 是否键盘操控
private float rotation;
private float angle;
private float dotValue;
private float rSpeed = 5;
private Vector3 targetDirection;
private Vector3 hitPoint;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
public enum PlayerState
{
idle = 0,
walk = 1,
speak = 2,
rotate = 3
}
public PlayerState playerState = PlayerState.idle;
void Start()
{
player = transform;
controller = player.gameObject.GetComponent<CharacterController>();
}
void Update()
{
MouseHandlePlayer();
if (Input.GetAxis("Vertical") > 0.1f || Input.GetAxis("Horizontal")!= 0)
{
isKey = true;
jianTou.gameObject.SetActive(false);
}
if (isKey)
{
// 键盘操控
KeyHandlePlayer();
}
else
{
// 鼠标点击时角色旋转
if (playerState == PlayerState.rotate)
{
dotValue = Vector3.Dot(player.forward.normalized, targetDirection.normalized);
angle = Mathf.Acos(dotValue) * Mathf.Rad2Deg;
player.rotation = Quaternion.Slerp(player.rotation, Quaternion.LookRotation(targetDirection), rSpeed * Time.deltaTime);
if (Mathf.Abs(angle) < 5)
{
playerState = PlayerState.walk;
}
}
else if (playerState == PlayerState.walk)
{
PlayerAnmi(playerState);
PlayerMove(isKey);
}
}
}
/// <summary>
/// 鼠标操控
/// </summary>
void MouseHandlePlayer()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit,20))
{
isKey = false;
hitPoint = new Vector3(hit.point.x, player.position.y, hit.point.z);
targetDirection = hitPoint - player.position;
playerState = PlayerState.rotate;
SetJianTou();
}
}
}
/// <summary>
/// 键盘操控
/// </summary>
void KeyHandlePlayer()
{
if (Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") <= 0)
{
return;
}
if (Input.GetAxis("Vertical") > 0.1f)
{
playerState = PlayerState.walk;
PlayerMove(isKey);
PlayerAnmi(playerState);
}
else
{
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0.1f)
{
rotation = Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime;
player.Rotate(0, rotation, 0);
}
playerState = PlayerState.idle;
PlayerAnmi(playerState);
}
}
void PlayerAnmi(PlayerState state)
{
switch (state)
{
case PlayerState.idle:
player.gameObject.animation.CrossFade("idle", 0.3f);
break;
case PlayerState.walk:
player.gameObject.animation.CrossFade("walk", 0.3f);
break;
case PlayerState.speak:
break;
}
}
void PlayerMove(bool _isKey)
{
if (_isKey)
{
moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
}
else
{
if (Mathf.Abs(Vector3.Distance(hitPoint, player.position)) < 0.5f)
{
jianTou.gameObject.SetActive(false);
isKey = true;
playerState = PlayerState.idle;
PlayerAnmi(playerState);
return;
}
moveDirection = new Vector3(0, 0, 1);
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
controller.SimpleMove(moveDirection );
}
/// <summary>
/// 点击地面箭头显示
/// </summary>
void SetJianTou()
{
jianTou.gameObject.SetActive(true);
jianTou.position = new Vector3(hitPoint.x, hitPoint.y, hitPoint.z);
}
}
using UnityEngine;
using System.Collections;
public class JianTouShow : MonoBehaviour {
private float startTime;
private int textureInt;
private float timeRate = 1;
public Texture2D[] jianTouList = new Texture2D[3];
void Update () {
JianTou();
}
void JianTou()
{
startTime += Time.deltaTime;
if (startTime > timeRate)
{
startTime = 0;
textureInt++;
if (textureInt >= jianTouList.Length)
{
textureInt = 0;
}
}
renderer.material.mainTexture = jianTouList[textureInt];
}
}