一.transform 移动受刚体碰撞
void Update () {
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
transform.Translate(new Vector3(h, 0, v) * speed*Time.deltaTime);
}
//刚体运动效果,可以碰撞物体效果
// rigidbody.MovePosition(transform.position + new Vector3(h, 0, v) * speed * Time.deltaTime);
二.摄像机跟随玩家视角
private Transform player;
private float smoothing = 3;
void Start () {
player = GameObject.FindGameObjectWithTag(Tags.player).transform;
}
// Update is called once per frame
void Update () {
Vector3 targetPos = player.position + new Vector3(3.7f, 1.4f, -4.1f);
transform.position = Vector3.Lerp(transform.position, targetPos, smoothing * Time.deltaTime);
}
三.得到层的代码
groundLayerIndex = LayerMask.GetMask("Ground");
四.控制转向
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if( Physics.Raycast(ray, out hitInfo, 200, groundLayerIndex))
{
Vector3 target = hitInfo.point;
target.y = transform.position.y;
transform.LookAt(target);
}
五.延迟 0.05秒执行ClearEffect 函数
<span style="font-size:18px;">Invoke("ClearEffect", 0.05f);</span>
六.射线的碰撞与方向
private LineRenderer lineRenderer;
lineRenderer = this.renderer as LineRenderer;
//
this.lineRenderer.enabled = true;
//开始点
lineRenderer.SetPosition(0, transform.position);
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hitInfo;
//结束点
if (Physics.Raycast(ray, out hitInfo))
{
lineRenderer.SetPosition(1, hitInfo.point);
}
else
{
lineRenderer.SetPosition(1, transform.position + transform.forward * 100);
}
七.禁用脚本
private PlayerShoot playerShoot;
//获取脚本
playerShoot = this.GetComponentInChildren<PlayerShoot>();
//禁止使用脚本
this.playerShoot.enabled = false;
八.导航网格
//先Bake,再添加NavMeshAgent
private NavMeshAgent agent;
agent = this.GetComponent<NavMeshAgent>();
agent.SetDestination(player.position);