鼠标移动

导航移动 1.地形设置静态-烘焙路线
2.导航的对象,添加NavMeshAgent组件
3.写代码,设置目标

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class PlayerMouseMove02 : MonoBehaviour {
    public GameObject arow;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        //GetComponent<组件>().
        //velocity速度以每秒距离单位(与物理相同)表示,并以全局坐标系表示。
        print(GetComponent<NavMeshAgent>().speed + ":speed");
        print(GetComponent<NavMeshAgent>().velocity + ":velocity");
        //magnitude 点和原点之间的距离(x, y, z)->(0, 0, 0).
        GetComponent<Animator>().SetBool("isRun", GetComponent <NavMeshAgent >().velocity.magnitude>0);
        GetComponent<Animator>().SetFloat("Blend", GetComponent<NavMeshAgent>().velocity.magnitude );
        if (Input.GetMouseButton (0))
        {
            //发射射线
            //静态属性-1.全局唯一的属性,你可以通过类名很方便的调用。
            //如果是对象的属性,你就要先找到对象,再调用属性,很麻烦
            //例GameObject.Find();
            //ViewportPointToRay是比例,mousePosition是像素
            //Ray 射线
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition );
            //RaycastHit 结构用于从光线广播中获取信息。
            RaycastHit hit;
            //Physics.Raycast:投射光线,从点开始origin方向direction,长度maxDistance与现场的所有对撞机相撞。 您可以选择提供层面具,以过滤掉任何您不感兴趣的对撞机产生碰撞。
            //out 是输出参数的关键字
            if (Physics.Raycast(ray,out hit,100f))
            {

                //判断是否碰撞倒了地面 collider所有对撞机的基类。
                if (hit.collider.tag=="Ground")
                {
                    //transform.LookAt:旋转变换,使前向矢量指向/目标/的当前位置。
                    transform.LookAt(hit.point);
                    Instantiate(arow, hit.point, Quaternion.identity);
                    GetComponent<NavMeshAgent>().SetDestination(hit.point );
                    
                }
            }
        }
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值