Luke Stampfli
4 年前
当前提交
208a3a8a
共有 17 个文件被更改,包括 393 次插入 和 7 次删除
-
64Assets/Prefabs/PlayerCharacter.prefab
-
58Assets/Scenes/SampleScene.unity
-
8Assets/Scenes/SampleScene.meta
-
2Assets/Scenes/SampleScene/NavMesh.asset
-
8Assets/Scenes/SampleScene/NavMesh.asset.meta
-
77Assets/Scripts/Character.cs
-
11Assets/Scripts/Character.cs.meta
-
25Assets/Scripts/Interpolation.cs
-
11Assets/Scripts/Interpolation.cs.meta
-
40Assets/Scripts/PlayerInput.cs
-
11Assets/Scripts/PlayerInput.cs.meta
-
14Assets/Scripts/PositionInterpolation.cs
-
11Assets/Scripts/PositionInterpolation.cs.meta
-
49Assets/Scripts/SimulationManager.cs
-
11Assets/Scripts/SimulationManager.cs.meta
|
|||
fileFormatVersion: 2 |
|||
guid: d35e52e80fddfa34e8598be81bb477bb |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
2
Assets/Scenes/SampleScene/NavMesh.asset
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
fileFormatVersion: 2 |
|||
guid: c7720ceef5c5df34c98168df9d4f695e |
|||
NativeFormatImporter: |
|||
externalObjects: {} |
|||
mainObjectFileID: 0 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using MLAPI; |
|||
using System; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using UnityEngine; |
|||
using UnityEngine.AI; |
|||
|
|||
public class Character : NetworkedBehaviour |
|||
{ |
|||
[SerializeField] |
|||
private float movementSpeed; |
|||
|
|||
private NavMeshAgent _navMeshAgent; |
|||
private NavMeshPath _path; |
|||
|
|||
private PositionInterpolation _interpolation; |
|||
|
|||
public void Awake() |
|||
{ |
|||
_path = new NavMeshPath(); |
|||
_navMeshAgent = GetComponent<NavMeshAgent>(); |
|||
_interpolation = GetComponentInChildren<PositionInterpolation>(); |
|||
} |
|||
|
|||
///<inheritdoc/>
|
|||
public override void NetworkStart() |
|||
{ |
|||
base.NetworkStart(); |
|||
|
|||
SimulationManager.Singleton.OnSimulationUpdate += SimulationUpdate; |
|||
SimulationManager.Singleton.OnSyncNetworkEvents += SyncNetworkEvents; |
|||
|
|||
_navMeshAgent.updateRotation = false; |
|||
_navMeshAgent.updatePosition = true; |
|||
|
|||
if (!IsServer) |
|||
{ |
|||
Destroy(_navMeshAgent); |
|||
} |
|||
|
|||
SetMovementTarget(new Vector3(5, 0, 5)); |
|||
} |
|||
|
|||
private void SyncNetworkEvents(float time, float deltaTime) |
|||
{ |
|||
_interpolation.AddValue(time, transform.position); |
|||
} |
|||
|
|||
public void SetMovementTarget(Vector3 position) |
|||
{ |
|||
_navMeshAgent.CalculatePath(position, _path); |
|||
} |
|||
|
|||
private void SimulationUpdate(float time, float deltaTime) |
|||
{ |
|||
if (IsServer) |
|||
{ |
|||
NavigationMovement(deltaTime); |
|||
} |
|||
} |
|||
|
|||
private void NavigationMovement(float deltaTime) |
|||
{ |
|||
var corners = _path.corners; // TODO: maybe use non-alloc version
|
|||
|
|||
if (corners.Any() && Vector3.SqrMagnitude(corners[corners.Length - 1]- transform.position) < 1) |
|||
{ |
|||
_path = new NavMeshPath(); |
|||
corners = _path.corners; |
|||
} |
|||
|
|||
var direction = corners.Length > 1 ? (corners[1]- corners[0]).normalized : Vector3.zero; |
|||
var movement = direction * movementSpeed * deltaTime; |
|||
|
|||
_navMeshAgent.Move(movement); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 5ef335173735a554a98a17d4ef9b21c2 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using UnityEngine; |
|||
|
|||
public abstract class Interpolation<T> : MonoBehaviour |
|||
{ |
|||
public abstract Func<T, T, float, T> LerpFunction { get;} |
|||
|
|||
private (float, T) _last; |
|||
private (float, T) _previous; |
|||
|
|||
public T GetValueForTime(float time) |
|||
{ |
|||
float timeSincePrevious = time - _previous.Item1; |
|||
float t = (timeSincePrevious / (_last.Item1 - _previous.Item1) - 1f); |
|||
|
|||
return LerpFunction(_previous.Item2, _last.Item2, t); |
|||
} |
|||
|
|||
public void AddValue(float time, T value) |
|||
{ |
|||
_previous = _last; |
|||
_last = (time, value); |
|||
} |
|||
|
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 4be874942db508e4bb7c4481d0ffb727 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using MLAPI; |
|||
using MLAPI.Messaging; |
|||
using System; |
|||
using UnityEngine; |
|||
|
|||
public class PlayerInput : NetworkedBehaviour |
|||
{ |
|||
private Character character; |
|||
|
|||
public void Awake() |
|||
{ |
|||
character = GetComponent<Character>(); |
|||
} |
|||
|
|||
public override void NetworkStart() |
|||
{ |
|||
if (IsClient) |
|||
{ |
|||
SimulationManager.Singleton.OnSimulationUpdate += SimulationUpdate; |
|||
} |
|||
} |
|||
|
|||
private void SimulationUpdate(float time, float deltaTime) |
|||
{ |
|||
if (Input.GetMouseButton(0)) |
|||
{ |
|||
RaycastHit hit; |
|||
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)) |
|||
{ |
|||
InvokeServerRpc(SendPlayerInput, hit.point); |
|||
} |
|||
} |
|||
} |
|||
|
|||
[ServerRPC] |
|||
public void SendPlayerInput(Vector3 position) |
|||
{ |
|||
character.SetMovementTarget(position); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 0eaaa5f4790dfc64ebc62ee85984bab9 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using UnityEngine; |
|||
using MLAPI; |
|||
|
|||
[DefaultExecutionOrder(200)] |
|||
public class PositionInterpolation : Interpolation<Vector3> |
|||
{ |
|||
public override Func<Vector3, Vector3, float, Vector3> LerpFunction => Vector3.LerpUnclamped; |
|||
|
|||
private void Update() |
|||
{ |
|||
transform.position = GetValueForTime(NetworkingManager.Singleton.NetworkTime); |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 7dac193e15c19dd47bcfb78c85bd6d8d |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using MLAPI; |
|||
using UnityEngine; |
|||
|
|||
[DefaultExecutionOrder(100)] |
|||
public class SimulationManager : MonoBehaviour |
|||
{ |
|||
public static SimulationManager Singleton { get; private set; } |
|||
|
|||
public delegate void SimulationUpdateDelegate(float time, float deltaTime); |
|||
|
|||
/// <summary>
|
|||
/// Main game simulation such as processing inputs, running AI, ticking state machines etc. An event is definitely not what we want to use because we have no order, but FixedUpdate isn't as well.
|
|||
/// </summary>
|
|||
|
|||
public event SimulationUpdateDelegate OnSimulationUpdate; |
|||
/// <summary>
|
|||
/// Called after simulation is done replicate game state such as positions to the client.
|
|||
/// </summary>
|
|||
public event SimulationUpdateDelegate OnSyncNetworkEvents; |
|||
|
|||
[SerializeField] |
|||
private int tickRate = 20; |
|||
|
|||
private float _networkTickDelta; |
|||
private float _lastNetworkTickTime; |
|||
|
|||
private void OnEnable() |
|||
{ |
|||
Singleton = this; |
|||
_networkTickDelta = 1f / tickRate; |
|||
} |
|||
|
|||
|
|||
// Update is called once per frame
|
|||
void Update() |
|||
{ |
|||
|
|||
// The goal here is to sync this up with MLAPIs synchronization but the current network tick model in MLAPI is broken.
|
|||
while (NetworkingManager.Singleton.NetworkTime - _lastNetworkTickTime >= _networkTickDelta) |
|||
{ |
|||
_lastNetworkTickTime += _networkTickDelta; |
|||
|
|||
Physics.Simulate(_networkTickDelta); |
|||
OnSimulationUpdate?.Invoke(_lastNetworkTickTime,_networkTickDelta); |
|||
|
|||
OnSyncNetworkEvents?.Invoke(_lastNetworkTickTime, _networkTickDelta); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 3e6c31da8c35c2743b0311f50e3e1a69 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue