|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEngine.AI; |
| 5 | + |
| 6 | +namespace MLAPI.MonoBehaviours.Prototyping |
| 7 | +{ |
| 8 | + public class NetworkedNavMeshAgent : NetworkedBehaviour |
| 9 | + { |
| 10 | + private NavMeshAgent agent; |
| 11 | + public bool EnableProximity = false; |
| 12 | + public float ProximityRange = 50f; |
| 13 | + public float CorrectionDelay = 3f; |
| 14 | + |
| 15 | + private static byte[] stateUpdateBuffer = new byte[36]; |
| 16 | + private static byte[] correctionBuffer = new byte[24]; |
| 17 | + |
| 18 | + private void Awake() |
| 19 | + { |
| 20 | + agent = GetComponent<NavMeshAgent>(); |
| 21 | + } |
| 22 | + |
| 23 | + public override void NetworkStart() |
| 24 | + { |
| 25 | + if (isClient) |
| 26 | + { |
| 27 | + RegisterMessageHandler("MLAPI_OnNavMeshStateUpdate", OnNavMeshStateUpdate); |
| 28 | + RegisterMessageHandler("MLAPI_OnNavMeshCorrectionUpdate", OnNavMeshCorrectionUpdate); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private Vector3 lastDestination = Vector3.zero; |
| 33 | + private float lastCorrectionTime = 0f; |
| 34 | + private void Update() |
| 35 | + { |
| 36 | + if (!isServer) |
| 37 | + return; |
| 38 | + |
| 39 | + if(agent.destination != lastDestination) |
| 40 | + { |
| 41 | + lastDestination = agent.destination; |
| 42 | + using (MemoryStream stream = new MemoryStream(stateUpdateBuffer)) |
| 43 | + { |
| 44 | + using (BinaryWriter writer = new BinaryWriter(stream)) |
| 45 | + { |
| 46 | + writer.Write(agent.destination.x); |
| 47 | + writer.Write(agent.destination.y); |
| 48 | + writer.Write(agent.destination.z); |
| 49 | + |
| 50 | + writer.Write(agent.velocity.x); |
| 51 | + writer.Write(agent.velocity.y); |
| 52 | + writer.Write(agent.velocity.z); |
| 53 | + |
| 54 | + writer.Write(transform.position.x); |
| 55 | + writer.Write(transform.position.y); |
| 56 | + writer.Write(transform.position.z); |
| 57 | + } |
| 58 | + if (!EnableProximity) |
| 59 | + { |
| 60 | + SendToClientsTarget("MLAPI_OnNavMeshStateUpdate", "MLAPI_NAV_AGENT_STATE", stream.GetBuffer()); |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + List<int> proximityClients = new List<int>(); |
| 65 | + foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients) |
| 66 | + { |
| 67 | + if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange) |
| 68 | + proximityClients.Add(client.Key); |
| 69 | + } |
| 70 | + SendToClientsTarget(proximityClients, "MLAPI_OnNavMeshStateUpdate", "MLAPI_NAV_AGENT_STATE", stream.GetBuffer()); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if(Time.time - lastCorrectionTime >= CorrectionDelay) |
| 76 | + { |
| 77 | + using (MemoryStream stream = new MemoryStream(correctionBuffer)) |
| 78 | + { |
| 79 | + using (BinaryWriter writer = new BinaryWriter(stream)) |
| 80 | + { |
| 81 | + writer.Write(agent.velocity.x); |
| 82 | + writer.Write(agent.velocity.y); |
| 83 | + writer.Write(agent.velocity.z); |
| 84 | + |
| 85 | + writer.Write(transform.position.x); |
| 86 | + writer.Write(transform.position.y); |
| 87 | + writer.Write(transform.position.z); |
| 88 | + } |
| 89 | + |
| 90 | + if (!EnableProximity) |
| 91 | + { |
| 92 | + SendToClientsTarget("MLAPI_OnNavMeshCorrectionUpdate", "MLAPI_NAV_AGENT_CORRECTION", stream.GetBuffer()); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + List<int> proximityClients = new List<int>(); |
| 97 | + foreach (KeyValuePair<int, NetworkedClient> client in NetworkingManager.singleton.connectedClients) |
| 98 | + { |
| 99 | + if (Vector3.Distance(client.Value.PlayerObject.transform.position, transform.position) <= ProximityRange) |
| 100 | + proximityClients.Add(client.Key); |
| 101 | + } |
| 102 | + SendToClientsTarget(proximityClients, "MLAPI_OnNavMeshCorrectionUpdate", "MLAPI_NAV_AGENT_CORRECTION", stream.GetBuffer()); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void OnNavMeshStateUpdate(int clientId, byte[] data) |
| 109 | + { |
| 110 | + using (MemoryStream stream = new MemoryStream(data)) |
| 111 | + { |
| 112 | + using (BinaryReader reader = new BinaryReader(stream)) |
| 113 | + { |
| 114 | + float xDestination = reader.ReadSingle(); |
| 115 | + float yDestination = reader.ReadSingle(); |
| 116 | + float zDestination = reader.ReadSingle(); |
| 117 | + |
| 118 | + float xVel = reader.ReadSingle(); |
| 119 | + float yVel = reader.ReadSingle(); |
| 120 | + float zVel = reader.ReadSingle(); |
| 121 | + |
| 122 | + float xPos = reader.ReadSingle(); |
| 123 | + float yPos = reader.ReadSingle(); |
| 124 | + float zPos = reader.ReadSingle(); |
| 125 | + |
| 126 | + Vector3 destination = new Vector3(xDestination, yDestination, zDestination); |
| 127 | + Vector3 velocity = new Vector3(xVel, yVel, zVel); |
| 128 | + Vector3 position = new Vector3(xPos, yPos, zPos); |
| 129 | + |
| 130 | + agent.SetDestination(destination); |
| 131 | + agent.velocity = velocity; |
| 132 | + agent.Warp(position); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private void OnNavMeshCorrectionUpdate(int clinetId, byte[] data) |
| 138 | + { |
| 139 | + using (MemoryStream stream = new MemoryStream(data)) |
| 140 | + { |
| 141 | + using (BinaryReader reader = new BinaryReader(stream)) |
| 142 | + { |
| 143 | + float xVel = reader.ReadSingle(); |
| 144 | + float yVel = reader.ReadSingle(); |
| 145 | + float zVel = reader.ReadSingle(); |
| 146 | + |
| 147 | + float xPos = reader.ReadSingle(); |
| 148 | + float yPos = reader.ReadSingle(); |
| 149 | + float zPos = reader.ReadSingle(); |
| 150 | + |
| 151 | + Vector3 velocity = new Vector3(xVel, yVel, zVel); |
| 152 | + Vector3 position = new Vector3(xPos, yPos, zPos); |
| 153 | + |
| 154 | + agent.velocity = velocity; |
| 155 | + agent.Warp(position); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments