目录
45、Navmesh路径
更新PlayerBot.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using Random = UnityEngine.Random;
public class PlayerBot : Player
{
……
void FixedUpdate()
{
if (isDead) return;
if (inRange.Count == 0)
{
if (Vector3.Distance(transform.position, targetPoint) < agent.stoppingDistance)
{
RandomPoint(transform.position, range * 3, out targetPoint);
}
}
else
{
if (Vector3.Distance(transform.position, targetPoint) < agent.stoppingDistance)
{
RandomPoint(inRange[0].transform.position, range * 2, out targetPoint);
}
for (int i = 0; i < inRange.Count; ++i)
{
RaycastHit hit;
if (Physics.Linecast(transform.position, inRange[i].transform.position, out hit))
{
Vector3 lookPos = inRange[i].transform.position;
turret.LookAt(lookPos);
turret.eulerAngles = new Vector3(-90, turret.eulerAngles.y, 0);
Vector3 shotDir = lookPos - transform.position;
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Shoot((short) (shotDir.x * 10), (short) (shotDir.z * 10));
}
break;
}
}
}
}
public void Shoot(short xPos, short zPos)
{
Vector3 shotCenter =
Vector3.Lerp(shotPos.position, new Vector3(xPos / 10f, shotPos.position.y, zPos / 10f), 0.6f);
// 创建子弹,并同步到客户端
GameObject obj = Instantiate(BulletPrefab, shotCenter, Quaternion.Euler(0, turret.eulerAngles.y, 0)); //把预制体实例化出子弹
obj.GetComponent<Bullet>().owner = gameObject;
Mirror.NetworkServer.Spawn(obj,obj.GetComponent<Mirror.NetworkIdentity>().assetId); // 在服务器端生成,并同步到客户端
}
protected override void RpcRespawn()
{
StartCoroutine(Respawn());
}
IEnumerator Respawn()
{
isDead = true;
inRange.Clear();
agent.isStopped = true;
ToggleComponents(false);
yield return new WaitForSeconds(GameManager.GetInstance().respawnTime);
ToggleComponents(true);
targetPoint = GameManager.GetInstance().GetSpawnPosition(teamIndex);
transform.position = targetPoint;
agent.Warp(targetPoint);
agent.isStopped = false;
isDead = false;
}
void ToggleComponents(bool state)
{
GetComponent<Rigidbody>().isKinematic = state;
GetComponent<Collider>().enabled = state;
for (int i = 0; i < transform.childCount; ++i)
{
transform.GetChild(i).gameObject.SetActive(state);
}
}
}
46、游戏开始界面制作
![](https://i-blog.csdnimg.cn/blog_migrate/7a3a11fb85ee26339015c0a4b2857b3b.png)
![](https://i-blog.csdnimg.cn/blog_migrate/717916ce481eff311c035e6fd2e7808f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ff375d4007b11917aaa6d1310c373bbf.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0bcdc9a9b0dbb93de184e68303dff171.png)
![](https://i-blog.csdnimg.cn/blog_migrate/dc888181d9e120acf4f68d6afdbde620.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8b9700948584227e36524c9d80a0da1f.png)
点击应用后
![](https://i-blog.csdnimg.cn/blog_migrate/c8071faa5c98e61e9e271f821d69a315.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3d509456138684a8bed62bd1e286bc73.png)
优化按钮UI
![](https://i-blog.csdnimg.cn/blog_migrate/1d6c980a73085d185a335d246d09e12c.png)
![](https://i-blog.csdnimg.cn/blog_migrate/99c6bcf02bad6ce15dfccb176bc738f3.png)
效果
![](https://i-blog.csdnimg.cn/blog_migrate/ef45957ca6310fe6a27e1f466f9160e5.png)
【
完结
】