You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ReactRacetrack/Assets/Scripts/DollyCameraController.cs

62 lines
1.5 KiB
C#

1 month ago
using UnityEngine;
using Cinemachine;
using System.Collections.Generic;
public class DollyCameraController : MonoBehaviour
{
public CinemachineDollyCart dollyCart;
public float tapWindowSeconds = 0.5f;
public float baseSpeed = 2f;
public float maxSpeed = 10f;
public float acceleration = 5f;
private List<float> tapTimestamps = new List<float>();
private float currentSpeed = 0f;
private float targetSpeed = 0f;
void Update()
{
HandleInput();
UpdateSpeed();
MoveCamera();
}
void HandleInput()
{
if (Input.GetMouseButtonDown(0) || Input.touchCount > 0)
{
tapTimestamps.Add(Time.time);
}
}
public float tapSensitivity = 0.8f;
void UpdateSpeed()
{
float cutoff = Time.time - tapWindowSeconds;
tapTimestamps.RemoveAll(t => t < cutoff);
float tps = tapTimestamps.Count / tapWindowSeconds;
// Add tap influence to base speed (softly)
float rawTarget = baseSpeed + (tps * tapSensitivity);
targetSpeed = Mathf.Clamp(rawTarget, baseSpeed, maxSpeed);
currentSpeed = Mathf.MoveTowards(currentSpeed, targetSpeed, acceleration * Time.deltaTime);
}
void MoveCamera()
{
if (dollyCart == null) return;
float cameraSpeed = currentSpeed / 50f;
dollyCart.m_Position += cameraSpeed * Time.deltaTime;
dollyCart.m_Position %= 1f;
}
public float GetCameraPathPosition()
{
return dollyCart.m_Position;
}
}