Unity3D(一)鼠标点击 物体移动
原视频链接:https://www.youtube.com/watch?v=RHwkTKbLQus
首先,根据视频中讲解,首先自然是创建地形terrain;
选定多个物体作为static 摘出Navgation
(这一步必须要有,而且后期可以加上一些障碍物为static,nav mesh agent模块可以让game object自动避障)
创建Game Object 给出C#
然后main Camera一定要设main标签
代码写进去就ok
game object可以加上材质material让其更为突出
其中的一些代码如果不能运行,百度,搞出新代码就ok
speed调节game object移动速度
下面代码 ClickToMove引用给gameobject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClickToMove : MonoBehaviour {
UnityEngine.AI.NavMeshAgent navAgent;
// Use this for initialization
void Start()
{
navAgent = GetComponent<UnityEngine.AI.NavMeshAgent>(); //获取组件
}
//private void Awake(){]
// Update is called once per frame
void Update()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonUp(0))
{
if (Physics.Raycast(ray, out hit, 500))
{
navAgent.SetDestination(hit.point);
}
}
}
}