This commit is contained in:
Karrar
2024-03-27 22:26:13 +03:00
commit 0caf47a728
223 changed files with 32253 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
using UnityEngine;
using Netick;
using Netick.Unity;
namespace Netick.Samples.FPS
{
public class FPSController : NetworkBehaviour
{
[SerializeField]
private float _movementSpeed = 10;
[SerializeField]
private float _sensitivityX = 1.6f;
[SerializeField]
private float _sensitivityY = -1f;
[SerializeField]
private Transform _cameraParent;
private CharacterController _CC;
private Vector2 _camAngles;
// Networked properties
[Networked][Smooth]
public Vector2 YawPitch { get; set; }
public override void NetworkStart()
{
_CC = GetComponent<CharacterController>();
if (IsInputSource)
{
var cam = Sandbox.FindObjectOfType<Camera>();
cam.transform.parent = _cameraParent;
cam.transform.localPosition = Vector3.zero;
cam.transform.localRotation = Quaternion.identity;
}
}
public override void OnInputSourceLeft()
{
// destroy the player object when its input source (controller player) leaves the game
Sandbox.Destroy(Object);
}
public override void NetworkUpdate()
{
if (!IsInputSource || !Sandbox.InputEnabled)
return;
Vector2 input = new Vector2(Input.GetAxisRaw("Mouse X") * _sensitivityX, Input.GetAxisRaw("Mouse Y") * _sensitivityY);
var networkInput = Sandbox.GetInput<FPSInput>();
networkInput.YawPitch += input;
Sandbox.SetInput<FPSInput>(networkInput);
// we apply the rotation in update too to have smooth camera control
_camAngles = ClampAngles(_camAngles.x + input.x, _camAngles.y + input.y);
ApplyRotations(_camAngles);
}
public override void NetworkFixedUpdate()
{
if (FetchInput(out FPSInput input))
MoveAndRotate(input);
}
private void MoveAndRotate(FPSInput input)
{
// rotation
YawPitch = ClampAngles(YawPitch.x + input.YawPitch.x, YawPitch.y + input.YawPitch.y);
ApplyRotations(YawPitch);
// movement direction
var movement = transform.TransformVector(new Vector3(input.Movement.x, 0, input.Movement.y)) * _movementSpeed;
movement.y = 0;
var gravity = 15f * Vector3.down;
// move
_CC.Move((movement + gravity) * Sandbox.FixedDeltaTime);
}
[OnChanged(nameof(YawPitch), invokeDuringResimulation: true)]
private void OnYawPitchChanged(OnChangedData onChanged)
{
ApplyRotations(YawPitch);
}
public override void NetworkRender()
{
if (IsProxy)
ApplyRotations(YawPitch);
}
private void ApplyRotations(Vector2 camAngles)
{
// on the player transform, we apply yaw
transform.rotation = Quaternion.Euler(new Vector3(0, camAngles.x, 0));
// on the weapon/camera holder, we apply the pitch angle
_cameraParent.localEulerAngles = new Vector3(camAngles.y, 0, 0);
_camAngles = camAngles;
}
private Vector2 ClampAngles(float yaw, float pitch)
{
return new Vector2(ClampAngle(yaw, -360, 360), ClampAngle(pitch, -80, 80));
}
private float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 754e582306c60a24eb1619b9878f55fd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,30 @@
using UnityEngine;
using Netick;
using Netick.Unity;
namespace Netick.Samples.FPS
{
public class FPSEventsHandler : NetworkEventsListener
{
public Transform SpawnPos;
public GameObject PlayerPrefab;
// This is called to read inputs.
public override void OnInput(NetworkSandbox sandbox)
{
var input = sandbox.GetInput<FPSInput>();
input.Movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
input.ShootInput |= Input.GetMouseButton(0);
sandbox.SetInput<FPSInput>(input);
}
// This is called on the server when a player has connected.
public override void OnPlayerConnected(NetworkSandbox sandbox, NetworkPlayer networkPlayer)
{
var spawnPos = SpawnPos.position + Vector3.left * (1 + sandbox.ConnectedPlayers.Count);
var player = sandbox.NetworkInstantiate(PlayerPrefab, spawnPos, Quaternion.identity, networkPlayer).GetComponent<FPSController>();
networkPlayer.PlayerObject = player.gameObject;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fb5a4e3a1e885b04e8f36bf0b0597274
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,12 @@
using UnityEngine;
using Netick;
namespace Netick.Samples.FPS
{
public struct FPSInput : INetworkInput
{
public Vector2 YawPitch;
public Vector2 Movement;
public NetworkBool ShootInput;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 98d08627104b50542980cdb39ff586af
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: