FirstPerson Camera
This commit is contained in:
1 parent
f0bb83c869
commit
ee8bd4799a
10 files changed
+42
-155
No files matched your search
@@ -1,15 +0,0 @@
|
|||||||
using TinyGoose.Tremble;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace Code.Game.OfficeGame.Location
|
|
||||||
{
|
|
||||||
[BrushEntity(type: BrushType.Trigger)]
|
|
||||||
public class LocationCameraTrigger : MonoBehaviour, IOnImportFromMapEntity
|
|
||||||
{
|
|
||||||
private int _id;
|
|
||||||
public void OnImportFromMapEntity(MapBsp mapBsp, BspEntity entity)
|
|
||||||
{
|
|
||||||
gameObject.layer = LayerMask.NameToLayer("CameraZone");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 8664bf69f30d4f558a6ab68b65c0ab8f
|
|
||||||
timeCreated: 1790280158
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 630e34c01f57474894fcdb9490d516d9
|
|
||||||
timeCreated: 1790332570
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
using TinyGoose.Tremble;
|
|
||||||
using Unity.VisualScripting;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace Code.Game.OfficeGame.Location
|
|
||||||
{
|
|
||||||
[PointEntity(category: "Utility", trenchBroomName: "LocationTransitionPoint")]
|
|
||||||
public class LocationTransitionPoint : MonoBehaviour
|
|
||||||
{
|
|
||||||
public LocationIDs locationID;
|
|
||||||
[NoTremble] [DoNotSerialize]
|
|
||||||
public LocationIDs selfLocationID;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: d3fe671e3d374beb9c03247bc7252cf3
|
|
||||||
timeCreated: 1790332908
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: 91456def45ba47a7bb82f0b9f97a6101
|
|
||||||
timeCreated: 1790335988
|
|
||||||
@@ -1,62 +0,0 @@
|
|||||||
using Code.Infrastructure.Camera;
|
|
||||||
using Code.Infrastructure.Camera.CameraStates;
|
|
||||||
using Cysharp.Threading.Tasks;
|
|
||||||
using LitMotion;
|
|
||||||
using LitMotion.Extensions;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace Code.OfficeGame.Player
|
|
||||||
{
|
|
||||||
[Infrastructure.Generators.State(typeof(CameraMode), typeof(CameraStateMachineContext), ManualInherit = true)]
|
|
||||||
public partial class LocationTransitionState : BaseCameraState
|
|
||||||
{
|
|
||||||
public void SetTarget(Transform target)
|
|
||||||
{
|
|
||||||
_target = target;
|
|
||||||
Context.cameraLoadedData.Camera.transform.position = _target.position;
|
|
||||||
Context.cameraLoadedData.Camera.transform.rotation = _target.rotation;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Transform _target;
|
|
||||||
private float _smoothTime = 0.15f;
|
|
||||||
private Vector3 _velocity;
|
|
||||||
|
|
||||||
public LocationTransitionState(CameraStateMachineContext context) : base(context)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override CameraMode MyState => CameraMode.LocationTransition;
|
|
||||||
|
|
||||||
public override UniTask EnterState()
|
|
||||||
{
|
|
||||||
return UniTask.CompletedTask;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async UniTask ExitState()
|
|
||||||
{
|
|
||||||
await UniTask.WaitUntil(() => _target);
|
|
||||||
|
|
||||||
var taskPosition = LMotion
|
|
||||||
.Create(Context.cameraLoadedData.Camera.transform.position, _target.position, 0.5f)
|
|
||||||
.BindToPosition(Context.cameraLoadedData.Camera.transform)
|
|
||||||
.ToUniTask();
|
|
||||||
|
|
||||||
var taskRotation = LMotion
|
|
||||||
.Create(Context.cameraLoadedData.Camera.transform.rotation, _target.rotation, 0.5f)
|
|
||||||
.BindToRotation(Context.cameraLoadedData.Camera.transform)
|
|
||||||
.ToUniTask();
|
|
||||||
|
|
||||||
await UniTask.WhenAll(taskPosition, taskRotation);
|
|
||||||
|
|
||||||
_target = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Update()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void LateUpdate()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
fileFormatVersion: 2
|
|
||||||
guid: ed824cfd1fc54e00bc51298e87d21197
|
|
||||||
timeCreated: 1790339775
|
|
||||||
@@ -1,38 +1,55 @@
|
|||||||
|
using System;
|
||||||
using Code.Infrastructure.Camera;
|
using Code.Infrastructure.Camera;
|
||||||
using Code.Infrastructure.Camera.CameraStates;
|
using Code.Infrastructure.Camera.CameraStates;
|
||||||
|
using Code.Infrastructure.Generators;
|
||||||
|
using Code.Infrastructure.Input;
|
||||||
using Cysharp.Threading.Tasks;
|
using Cysharp.Threading.Tasks;
|
||||||
|
using R3;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.InputSystem;
|
||||||
|
|
||||||
namespace Code.Game.Gameplay.Player
|
namespace Code.Game.Gameplay.Player
|
||||||
{
|
{
|
||||||
[Infrastructure.Generators.State(typeof(CameraMode), typeof(CameraStateMachineContext), ManualInherit = true)]
|
[State(typeof(CameraMode),typeof(CameraStateMachineContext), ManualInherit = true)]
|
||||||
public partial class OpenMovementCameraState : BaseCameraState
|
public partial class CleanerCameraState : BaseCameraState
|
||||||
{
|
{
|
||||||
public void SetTarget(Transform target)
|
public void SetTarget(Transform target)
|
||||||
{
|
{
|
||||||
_target = target;
|
_target = target;
|
||||||
Context.cameraLoadedData.Camera.transform.position = _target.position + _offset;
|
Context.cameraLoadedData.Camera.transform.position = _target.position;
|
||||||
Context.cameraLoadedData.Camera.transform.rotation = Quaternion.Euler(_rotationOffset);
|
Context.cameraLoadedData.Camera.transform.rotation = _target.rotation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private Transform _target;
|
private Transform _target;
|
||||||
private Vector3 _offset = new(0f, 3.5f, -6f);
|
private Vector2 _mouseDelta;
|
||||||
private Vector3 _rotationOffset = new(30f, 0, 0);
|
private Vector2 _rotation = Vector2.zero;
|
||||||
private int _cameraZoneMask = LayerMask.GetMask("CameraZone");
|
|
||||||
|
private float _sensitivityX = 0.1f;
|
||||||
|
private float _sensitivityY = 0.1f;
|
||||||
|
private float _slideStupidity = 1f;
|
||||||
|
private bool _isSliding;
|
||||||
|
|
||||||
public OpenMovementCameraState(CameraStateMachineContext context) : base(context)
|
public CleanerCameraState(CameraStateMachineContext context) : base(context)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void LookCancelled(InputAction.CallbackContext obj) => _mouseDelta = Vector2.zero;
|
||||||
|
private void LookPerformed(InputAction.CallbackContext obj) => _mouseDelta = obj.ReadValue<Vector2>();
|
||||||
|
|
||||||
public override CameraMode MyState => CameraMode.OpenMovement;
|
public override CameraMode MyState => CameraMode.OpenMovement;
|
||||||
|
|
||||||
public override UniTask EnterState()
|
public override UniTask EnterState()
|
||||||
{
|
{
|
||||||
|
Context.InputService.BindActionPerformed((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Watchment, LookPerformed);
|
||||||
|
Context.InputService.BindActionCancelled((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Watchment, LookCancelled);
|
||||||
return UniTask.CompletedTask;
|
return UniTask.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override UniTask ExitState()
|
public override UniTask ExitState()
|
||||||
{
|
{
|
||||||
|
Context.InputService.UnbindActionPerformed((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Watchment, LookPerformed);
|
||||||
|
Context.InputService.UnbindActionCancelled((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Watchment, LookCancelled);
|
||||||
return UniTask.CompletedTask;
|
return UniTask.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,53 +57,29 @@ namespace Code.Game.Gameplay.Player
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
private const float ZoneSearchRadius = 3f;
|
|
||||||
private static readonly Collider[] _zoneBuffer = new Collider[8];
|
|
||||||
|
|
||||||
public override void LateUpdate()
|
public override void LateUpdate()
|
||||||
{
|
{
|
||||||
if (_target is null) return;
|
if (_target is null) return;
|
||||||
|
|
||||||
var camTransform = Context.cameraLoadedData.Camera.transform;
|
var camTransform = Context.cameraLoadedData.Camera.transform;
|
||||||
var desired = _target.position + _offset;
|
|
||||||
|
|
||||||
int count = Physics.OverlapSphereNonAlloc(
|
camTransform.position = _target.position;
|
||||||
camTransform.position,
|
|
||||||
ZoneSearchRadius,
|
|
||||||
_zoneBuffer,
|
|
||||||
_cameraZoneMask,
|
|
||||||
QueryTriggerInteraction.Collide);
|
|
||||||
|
|
||||||
if (count == 0)
|
var mouseDeltaX = _mouseDelta.x * _sensitivityX;
|
||||||
|
var mouseDeltaY = _mouseDelta.y * _sensitivityY;
|
||||||
|
|
||||||
|
if (_isSliding)
|
||||||
{
|
{
|
||||||
if (Physics.CheckSphere(desired, 0.01f, _cameraZoneMask, QueryTriggerInteraction.Collide))
|
mouseDeltaX = Mathf.Clamp(mouseDeltaX, -_slideStupidity, _slideStupidity);
|
||||||
camTransform.position = desired;
|
mouseDeltaY = Mathf.Clamp(mouseDeltaY, -_slideStupidity, _slideStupidity);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_rotation.x += mouseDeltaX;
|
||||||
|
_rotation.y -= mouseDeltaY;
|
||||||
|
|
||||||
|
_rotation.y = Mathf.Clamp(_rotation.y, -90f, 90f);
|
||||||
|
|
||||||
var best = camTransform.position;
|
camTransform.localRotation = Quaternion.Euler(_rotation.y, _rotation.x, 0f);
|
||||||
var bestSqr = float.MaxValue;
|
|
||||||
|
|
||||||
for (int i = 0; i < count; i++)
|
|
||||||
{
|
|
||||||
var p = _zoneBuffer[i].ClosestPoint(desired);
|
|
||||||
var sqr = (p - desired).sqrMagnitude;
|
|
||||||
if (sqr < bestSqr)
|
|
||||||
{
|
|
||||||
bestSqr = sqr;
|
|
||||||
best = p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
camTransform.position = best;
|
|
||||||
}
|
}
|
||||||
// if (_target is null) return;
|
|
||||||
//
|
|
||||||
// var camTransform = Context.cameraLoadedData.Camera.transform;
|
|
||||||
//
|
|
||||||
// var camTargetPosition = _target.position + _offset;
|
|
||||||
//
|
|
||||||
// if (Physics.CheckSphere(camTargetPosition, 0.01f, _cameraZoneMask))
|
|
||||||
// camTransform.position = camTargetPosition;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: 9e592628c3fe45bf9659d1050680c296
|
guid: ff3e1673b49d4a4ab9a7017656796b6e
|
||||||
timeCreated: 1785846388
|
timeCreated: 1792340121
|
||||||
Reference in new issue
Block a user