鼠标点击地面,人物射线移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playMOVE : MonoBehaviour
{
    CharacterController cc;
    Vector3 post;
    void Start()
    {
        cc = GetComponent<CharacterController>();
    }
    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            //生成射线
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo) && hitInfo.collider.tag == "Ground")
            {
              //鼠标点击的位置
                post = hitInfo.point;
                //人物朝向
                transform.LookAt(new Vector3(post.x, transform.position.y, post.z));
               //实例化点击地面特效
                GameObject obj = Instantiate(Resources.Load("tx"), post, Quaternion.identity) as GameObject;
               //0.1秒后销毁
                Destroy(obj, 0.1f);
            }
        }
        if (post != Vector3.zero)
        {
            if (Vector3.Distance(post, transform.position) > 0.7f)
            {
                cc.SimpleMove(this.transform.forward * 5f);
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/itliruochong/article/details/80315419