new car camera

This commit is contained in:
Valera
2025-06-14 18:21:37 +07:00
parent 265af16061
commit 969c8e3bd0
4 changed files with 290 additions and 24 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;
@@ -19,7 +20,9 @@ namespace Sandbox
[Property] VeloXCar Car { get; set; }
[Property] public Vector3 CameraOffset { get; set; }
private Angles EyeAngles { get; set; }
public float blendSpeed = 10f;
private Angles EyeAngles = new( 0, 180, 0 );
private Rotation targetRotation;
protected override void OnUpdate()
{
@@ -34,7 +37,7 @@ namespace Sandbox
Angles input = Input.AnalogLook;
Angles eyeAngles = EyeAngles;
eyeAngles += input;
eyeAngles += input.WithPitch( -input.pitch );
eyeAngles.roll = 0f;
eyeAngles.pitch = eyeAngles.pitch.Clamp( 0f - 89, 89 );
@@ -46,8 +49,18 @@ namespace Sandbox
{
var cam = Scene.Camera;
cam.WorldRotation = EyeAngles;
cam.WorldPosition = WorldPosition + Vector3.Up * CameraOffset.z + cam.WorldRotation.Backward * CameraOffset.x;
var zoffset = Vector3.Up * CameraOffset.z;
Vector3 targetPos = WorldPosition + zoffset + WorldRotation * EyeAngles * CameraOffset;
float blendFactor = blendSpeed * Time.Delta;
blendFactor = MathF.Min( blendFactor, 1f );
cam.WorldPosition = Vector3.Lerp( cam.WorldPosition, targetPos, blendFactor );
Vector3 lookDirection = WorldPosition + zoffset - cam.WorldPosition;
if ( lookDirection != Vector3.Zero )
targetRotation = Rotation.LookAt( lookDirection, Vector3.Up );
cam.WorldRotation = targetRotation;
}
}
}