鼠标点击控制角色移动

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];
    }
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值