feature: PlayerDriver

This commit is contained in:
Alexandre Coirier
2026-05-19 13:46:31 +02:00
parent 0ca3cafe96
commit 578b94f1dc
4 changed files with 465 additions and 61 deletions
@@ -1,6 +1,8 @@
using OctoCore.Inputs;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UIElements;
namespace OctoCore.Components
{
@@ -19,6 +21,13 @@ namespace OctoCore.Components
public bool IsRunning;
public float Yaw;
public float Pitch;
[Space(10)]
public bool IsDriven;
[Range(0, 1)] public float DriventIntensity;
public Transform DriveTarget;
public Transform DriveLookAt;
}
[System.Serializable]
@@ -129,6 +138,61 @@ namespace OctoCore.Components
}
void Update()
{
HandleFocus();
HandleMovement();
}
public void Teleport(Vector3 position, Quaternion rotation)
{
State.IsPaused = _jumpInput = _jumpping = _runInput = false;
_runInertia = _jumpLerp = _jumpTime = _jumpAltitude = 0;
_moveInput = _lookInput = Vector2.zero;
_moveInertia = _moveDirection = Vector3.zero;
State.Yaw = rotation.eulerAngles.y;
State.Pitch = 0;
if (_references.Controller)
_references.Controller.enabled = false;
transform.position = position;
transform.rotation = rotation;
if (_references.Controller)
_references.Controller.enabled = true;
}
public void Pause(bool pause)
{
State.IsPaused = pause;
if (OctoCore.Instance)
OctoCore.Instance.UI.SetCursor();
}
public void Drive(Transform target, Transform lookAt)
{
State.DriveTarget = target;
State.DriveLookAt = lookAt;
State.IsDriven = true;
}
public void ReleaseDrive()
{
State.IsDriven = false;
State.DriveTarget = null;
State.DriveLookAt = null;
}
public void SetYaw(float yaw) { State.Yaw = yaw; }
public void SetPitch(float pitch) { State.Yaw = pitch; }
private void Move(Vector2 value) { _moveInput = value; }
private void Look(Vector2 value) { _lookInput = value; }
private void Run() { _runInput = true; }
private void Jump() { _jumpInput = true; }
private void HandleFocus()
{
bool escapePressed = false;
bool mouseClicked = false;
@@ -167,8 +231,10 @@ namespace OctoCore.Components
if (!_wasPausedBeforeUnfocuss)
Pause(false);
}
}
private void HandleMovement()
{
_moveInput = (State.IsPaused || !State.IsFocussed) ? Vector2.zero : Vector2.ClampMagnitude(_moveInput, 1);
Camera cam = (_firstPerson.Camera && _firstPerson.Camera.enabled && _firstPerson.Camera.gameObject.activeInHierarchy) ? _firstPerson.Camera : Camera.main;
@@ -193,6 +259,22 @@ namespace OctoCore.Components
move = camRight * move.x + camForward * move.z;
}
if (State.IsDriven)
{
State.DriventIntensity = Mathf.Clamp01(State.DriventIntensity + move.magnitude * t * 1);
Vector3 driveDir = State.DriveTarget.position - transform.position;
float driveAtten = 1 - Mathf.Clamp01(driveDir.magnitude);
Vector3 driveMove = driveDir;
driveMove.y = 0;
driveMove = driveMove.normalized * move.magnitude;
driveMove = Vector3.Lerp(driveMove, Vector3.zero, driveAtten);
move = Vector3.Lerp(move, driveMove, State.DriventIntensity);
}
float deltaSmooth = t * (accelerating ? 30 : 15);
_moveDirection = Vector3.Lerp(_moveDirection, move, deltaSmooth * 1.5f);
@@ -205,7 +287,7 @@ namespace OctoCore.Components
State.IsRunning = _runInertia > .1f;
Vector3 moveDelta = _moveInertia * t * Mathf.Lerp(_settings.WalkSpeed, _settings.RunSpeed, _runInertia);
if (isGrounded && _jumpInput && !_jumpping)
{
_jumpInput = false;
@@ -231,11 +313,11 @@ namespace OctoCore.Components
{
float x = _jumpLerp;
x = x * 2 - 1;
float y;
if (x < 0) y = Mathf.Sqrt(1f - x * x);
else y = Mathf.Cos(x * .5f * Mathf.PI);
y = _jumpAltitude + y * _settings.JumpHeight;
jumpOffset = y - transform.position.y;
@@ -267,8 +349,31 @@ namespace OctoCore.Components
State.Yaw += mouseX;
State.Pitch = Mathf.Clamp(State.Pitch + mouseY, _firstPerson.MinPitch, _firstPerson.MaxPitch);
transform.rotation = Quaternion.Euler(0, State.Yaw, 0);
cam.transform.localRotation = Quaternion.Euler(State.Pitch, 0, 0);
if (State.IsDriven)
{
Vector3 lookAt = State.DriveLookAt.position - _firstPerson.Camera.transform.position;
lookAt = lookAt.normalized;
Vector3 playerPos2D = transform.position;
Vector3 targetPos2D = State.DriveTarget.position;
playerPos2D.y = targetPos2D.y = 0;
float yaw = Mathf.Atan2(lookAt.x, lookAt.z) * Mathf.Rad2Deg;
float pitch = Mathf.Asin(-lookAt.y) * Mathf.Rad2Deg;
float lerp = 1 - Mathf.Clamp01(Vector3.Distance(playerPos2D, targetPos2D) / 2);
float yawRange = Mathf.Lerp(180, 0, lerp);
float pitchRange = Mathf.Lerp(90, 0, lerp);
State.Yaw = yaw + Mathf.Clamp(Mathf.DeltaAngle(yaw, State.Yaw), -yawRange, yawRange);
State.Pitch = pitch + Mathf.Clamp(Mathf.DeltaAngle(pitch, State.Pitch), -pitchRange, pitchRange);
}
Quaternion YawRot = Quaternion.Euler(0, State.Yaw, 0);
Quaternion PitchRot = Quaternion.Euler(State.Pitch, 0, 0);
transform.rotation = YawRot;
cam.transform.localRotation = PitchRot;
}
break;
@@ -296,41 +401,6 @@ namespace OctoCore.Components
_runInput = false;
}
public void Teleport(Vector3 position, Quaternion rotation)
{
State.IsPaused = _jumpInput = _jumpping = _runInput = false;
_runInertia = _jumpLerp = _jumpTime = _jumpAltitude = 0;
_moveInput = _lookInput = Vector2.zero;
_moveInertia = _moveDirection = Vector3.zero;
State.Yaw = rotation.eulerAngles.y;
State.Pitch = 0;
if (_references.Controller)
_references.Controller.enabled = false;
transform.position = position;
transform.rotation = rotation;
if (_references.Controller)
_references.Controller.enabled = true;
}
public void Pause(bool pause)
{
State.IsPaused = pause;
if (OctoCore.Instance)
OctoCore.Instance.UI.SetCursor();
}
public void SetYaw(float yaw) { State.Yaw = yaw; }
public void SetPitch(float pitch) { State.Yaw = pitch; }
private void Move(Vector2 value) { _moveInput = value; }
private void Look(Vector2 value) { _lookInput = value; }
private void Run() { _runInput = true; }
private void Jump() { _jumpInput = true; }
private void SetMode()
{
switch (_settings.Mode)
@@ -0,0 +1,31 @@
using UnityEngine;
namespace OctoCore.Components
{
public class OctoPlayerDriver : MonoBehaviour
{
[System.Serializable]
public class References
{
public Transform Target;
public Transform LookAt;
}
[SerializeField] private References _references;
void Start()
{
}
void Update()
{
}
public void DrivePlayer()
{
OctoPlayer.Instance.Drive(_references.Target, _references.LookAt);
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: cc0335304e1f8fc4588964a0aea84656