Развитие гоночек
This commit is contained in:
1 parent
ec60c47652
commit
b54083521f
532 files changed
+16091
-28231
No files matched your search
@@ -0,0 +1,174 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class RacingCamera : MonoBehaviour
|
||||
{
|
||||
public enum CameraMode
|
||||
{
|
||||
Follow,
|
||||
Orbit,
|
||||
Hood,
|
||||
Bumper
|
||||
}
|
||||
|
||||
[Header("Target")]
|
||||
public Transform carTarget;
|
||||
public Rigidbody carRigidbody;
|
||||
|
||||
[Header("Camera Mode")]
|
||||
public CameraMode currentMode = CameraMode.Follow;
|
||||
public KeyCode switchKey = KeyCode.C;
|
||||
|
||||
[Header("Follow Mode Settings")]
|
||||
public Vector3 followOffset = new Vector3(0, 3, -8);
|
||||
public float followSmoothTime = 0.1f;
|
||||
|
||||
[Header("Orbit Mode Settings")]
|
||||
public float orbitDistance = 10f;
|
||||
public float orbitHeight = 3f;
|
||||
public float orbitSpeed = 2f;
|
||||
private float orbitAngle = 0f;
|
||||
|
||||
[Header("First Person Settings")]
|
||||
public Transform hoodCameraPosition;
|
||||
public Transform bumperCameraPosition;
|
||||
|
||||
[Header("Look Settings")]
|
||||
public float lookAheadDistance = 5f;
|
||||
public Vector3 lookAtOffset = new Vector3(0, 1.5f, 0);
|
||||
|
||||
private Vector3 velocity = Vector3.zero;
|
||||
private Camera cam;
|
||||
|
||||
void Start()
|
||||
{
|
||||
cam = GetComponent<Camera>();
|
||||
if (carRigidbody == null && carTarget != null)
|
||||
carRigidbody = carTarget.GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Переключение режимов камеры
|
||||
if (Input.GetKeyDown(switchKey))
|
||||
{
|
||||
SwitchCameraMode();
|
||||
}
|
||||
}
|
||||
|
||||
void LateUpdate()
|
||||
{
|
||||
if (carTarget == null) return;
|
||||
|
||||
switch (currentMode)
|
||||
{
|
||||
case CameraMode.Follow:
|
||||
UpdateFollowCamera();
|
||||
break;
|
||||
|
||||
case CameraMode.Orbit:
|
||||
UpdateOrbitCamera();
|
||||
break;
|
||||
|
||||
case CameraMode.Hood:
|
||||
UpdateFirstPersonCamera(hoodCameraPosition);
|
||||
break;
|
||||
|
||||
case CameraMode.Bumper:
|
||||
UpdateFirstPersonCamera(bumperCameraPosition);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void SwitchCameraMode()
|
||||
{
|
||||
currentMode = (CameraMode)(((int)currentMode + 1) % 4);
|
||||
Debug.Log("Camera mode: " + currentMode);
|
||||
}
|
||||
|
||||
void UpdateFollowCamera()
|
||||
{
|
||||
// Рассчитываем позицию с учетом скорости
|
||||
Vector3 targetPosition = CalculateFollowPosition();
|
||||
|
||||
// Плавное перемещение
|
||||
transform.position = Vector3.SmoothDamp(
|
||||
transform.position,
|
||||
targetPosition,
|
||||
ref velocity,
|
||||
followSmoothTime
|
||||
);
|
||||
|
||||
// Направление взгляда с опережением
|
||||
Vector3 lookTarget = carTarget.position +
|
||||
carTarget.forward * lookAheadDistance +
|
||||
lookAtOffset;
|
||||
|
||||
transform.LookAt(lookTarget);
|
||||
}
|
||||
|
||||
Vector3 CalculateFollowPosition()
|
||||
{
|
||||
Vector3 basePosition = carTarget.position +
|
||||
carTarget.TransformDirection(followOffset);
|
||||
|
||||
// Динамическое смещение на основе скорости
|
||||
if (carRigidbody != null)
|
||||
{
|
||||
float speedFactor = Mathf.Clamp01(carRigidbody.linearVelocity.magnitude / 50f);
|
||||
Vector3 speedOffset = carTarget.forward * speedFactor * -3f;
|
||||
basePosition += speedOffset;
|
||||
}
|
||||
|
||||
return basePosition;
|
||||
}
|
||||
|
||||
void UpdateOrbitCamera()
|
||||
{
|
||||
// Автоматическое вращение вокруг машины
|
||||
orbitAngle += orbitSpeed * Time.deltaTime;
|
||||
|
||||
// Рассчитываем позицию по окружности
|
||||
float x = Mathf.Sin(orbitAngle) * orbitDistance;
|
||||
float z = Mathf.Cos(orbitAngle) * orbitDistance;
|
||||
|
||||
Vector3 orbitPosition = carTarget.position +
|
||||
new Vector3(x, orbitHeight, z);
|
||||
|
||||
transform.position = orbitPosition;
|
||||
transform.LookAt(carTarget.position + lookAtOffset);
|
||||
}
|
||||
|
||||
void UpdateFirstPersonCamera(Transform cameraPosition)
|
||||
{
|
||||
if (cameraPosition == null)
|
||||
{
|
||||
// Резервная позиция
|
||||
transform.position = carTarget.position +
|
||||
carTarget.forward * 1f +
|
||||
Vector3.up * 1.5f;
|
||||
transform.rotation = carTarget.rotation;
|
||||
return;
|
||||
}
|
||||
|
||||
transform.position = cameraPosition.position;
|
||||
transform.rotation = cameraPosition.rotation;
|
||||
|
||||
// Добавляем небольшую тряску на скорости
|
||||
if (carRigidbody != null)
|
||||
{
|
||||
float shakeAmount = carRigidbody.linearVelocity.magnitude * 0.001f;
|
||||
transform.position += Random.insideUnitSphere * shakeAmount;
|
||||
}
|
||||
}
|
||||
|
||||
// Визуализация в редакторе
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
if (carTarget != null)
|
||||
{
|
||||
Gizmos.color = Color.yellow;
|
||||
Gizmos.DrawWireSphere(carTarget.position + lookAtOffset, 0.5f);
|
||||
Gizmos.DrawLine(transform.position, carTarget.position + lookAtOffset);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1a6cd725a6ce4cc0b50b8a04be749bd1
|
||||
timeCreated: 1770228499
|
||||
@@ -18,7 +18,7 @@ namespace Code.Infrastructure.Camera.CameraStates
|
||||
protected override void OnExitState()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
protected override void HandlePosition(float deltaTime, object targetRotation)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
using Code.Infrastructure.Generators;
|
||||
using DG.Tweening;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Infrastructure.Camera.CameraStates
|
||||
{
|
||||
[State(typeof(CameraMode),typeof(CameraStateMachineContext), true)]
|
||||
public partial class ThirdPersonState : BaseCameraState
|
||||
{
|
||||
public ThirdPersonState(CameraStateMachineContext context, EventBus.EventBus eventBus) : base(context, eventBus)
|
||||
{
|
||||
}
|
||||
|
||||
public override CameraMode myState => CameraMode.ThirdPerson;
|
||||
protected override void OnEnterState()
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnExitState()
|
||||
{
|
||||
}
|
||||
|
||||
protected override void HandleRotationInput(float deltaTime, Vector3 rotationInput, out Quaternion q)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
private Transform _carTarget;
|
||||
private Rigidbody _carRigidbody;
|
||||
|
||||
public void SetFollowTarget(Transform carTarget, Rigidbody carRigidbody)
|
||||
{
|
||||
_carTarget = carTarget;
|
||||
_carRigidbody = carRigidbody;
|
||||
}
|
||||
|
||||
private Vector3 _lookAtOffset = new Vector3(0, 1.5f, 0);
|
||||
protected override void HandlePosition(float deltaTime, object targetRotation)
|
||||
{
|
||||
Fuck();
|
||||
}
|
||||
|
||||
private void Fuck()
|
||||
{
|
||||
// Автоматическое вращение вокруг машины
|
||||
Vector3 targetPosition = CalculateFollowPosition();
|
||||
|
||||
// Плавное перемещение
|
||||
|
||||
Vector3 velocity = _carRigidbody ? _carRigidbody.linearVelocity : new Vector3();
|
||||
// Context.cameraLoadedData.Camera.transform.DOMove(targetPosition,
|
||||
// Context.cameraLoadedData.SettingsConfigs[CameraMode.ThirdPerson].followSharpness);
|
||||
Context.cameraLoadedData.Camera.transform.position = Vector3.SmoothDamp(
|
||||
Context.cameraLoadedData.Camera.transform.position,
|
||||
targetPosition,
|
||||
ref velocity,
|
||||
Context.cameraLoadedData.SettingsConfigs[CameraMode.ThirdPerson].followSharpness
|
||||
);
|
||||
|
||||
// Направление взгляда с опережением
|
||||
Vector3 lookTarget = _carTarget.position +
|
||||
_carTarget.forward * Context.cameraLoadedData.SettingsConfigs[CameraMode.ThirdPerson].defaultDistance +
|
||||
_lookAtOffset;
|
||||
|
||||
Context.cameraLoadedData.Camera.transform.LookAt(lookTarget);
|
||||
}
|
||||
|
||||
private Vector3 _followOffset = new Vector3(0, 3, -8);
|
||||
|
||||
private Vector3 CalculateFollowPosition()
|
||||
{
|
||||
Vector3 basePosition = _carTarget.position +
|
||||
_carTarget.TransformDirection(_followOffset);
|
||||
|
||||
// Динамическое смещение на основе скорости
|
||||
if (!_carRigidbody) return basePosition;
|
||||
|
||||
var speedFactor = Mathf.Clamp01(_carRigidbody.linearVelocity.magnitude / 50f);
|
||||
var speedOffset = _carTarget.forward * (speedFactor * -3f);
|
||||
basePosition += speedOffset;
|
||||
|
||||
return basePosition;
|
||||
}
|
||||
public override void Update()
|
||||
{
|
||||
}
|
||||
|
||||
public override void LateUpdate()
|
||||
{
|
||||
Fuck();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 817473133bf54c7aab1ea3e093561e8e
|
||||
timeCreated: 1770227852
|
||||
Reference in new issue
Block a user