2023-07-24 16:38:13 +03:00
|
|
|
using Dreamteck.Splines;
|
|
|
|
using PathCreation;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
public class ShipPathFollower : MonoBehaviour
|
|
|
|
{
|
|
|
|
public float speed = 10f;
|
|
|
|
public float minSpeed = 1f;
|
|
|
|
public float maxSpeed = 20f;
|
|
|
|
public float frictionForce = 0.1f;
|
|
|
|
public float gravityForce = 1f;
|
|
|
|
public float slopeRange = 60f;
|
2023-08-01 11:28:14 +03:00
|
|
|
[SerializeField]
|
|
|
|
private SplineFollower follower;
|
2023-07-24 16:38:13 +03:00
|
|
|
public AnimationCurve speedGain;
|
|
|
|
public AnimationCurve speedLoss;
|
|
|
|
public float brakeSpeed = 0f;
|
|
|
|
public float brakeReleaseSpeed = 0f;
|
|
|
|
|
|
|
|
private float brakeTime = 0f;
|
|
|
|
private float brakeForce = 0f;
|
|
|
|
private float addForce = 0f;
|
|
|
|
|
|
|
|
public float soundFadeLength = 0.15f;
|
|
|
|
|
|
|
|
public event UnityAction<float> OnSpeedChange;
|
|
|
|
|
|
|
|
public InputActionReference LeftTrigger;
|
|
|
|
public InputActionReference RightTrigger;
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
if (Input.GetKey(KeyCode.W) || RightTrigger.action.inProgress && LeftTrigger.action.inProgress)
|
|
|
|
{
|
|
|
|
AddForce(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
float dot = Vector3.Dot(this.transform.forward, Vector3.down);
|
|
|
|
float dotPercent = Mathf.Lerp(-slopeRange / 90f, slopeRange / 90f, (dot + 1f) / 2f);
|
|
|
|
speed -= Time.deltaTime * frictionForce * (1f - brakeForce);
|
|
|
|
float speedAdd = 0f;
|
|
|
|
float speedPercent = Mathf.InverseLerp(minSpeed, maxSpeed, speed);
|
|
|
|
if (dotPercent > 0f)
|
|
|
|
{
|
|
|
|
speedAdd = gravityForce * dotPercent * speedGain.Evaluate(speedPercent) * Time.deltaTime;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
speedAdd = gravityForce * dotPercent * speedLoss.Evaluate(1f - speedPercent) * Time.deltaTime;
|
|
|
|
}
|
|
|
|
speed += speedAdd * (1f - brakeForce);
|
|
|
|
speed = Mathf.Clamp(speed, minSpeed, maxSpeed);
|
|
|
|
if (addForce > 0f)
|
|
|
|
{
|
|
|
|
float lastAdd = addForce;
|
|
|
|
addForce = Mathf.MoveTowards(addForce, 0f, Time.deltaTime * 30f);
|
|
|
|
speed += lastAdd - addForce;
|
|
|
|
}
|
|
|
|
follower.followSpeed = speed;
|
|
|
|
follower.followSpeed *= (1f - brakeForce);
|
2023-08-01 11:28:14 +03:00
|
|
|
|
|
|
|
if (brakeTime > Time.time)
|
|
|
|
brakeForce = Mathf.MoveTowards(brakeForce, 1f, Time.deltaTime * brakeSpeed);
|
|
|
|
else
|
|
|
|
brakeForce = Mathf.MoveTowards(brakeForce, 0f, Time.deltaTime * brakeReleaseSpeed);
|
2023-07-24 16:38:13 +03:00
|
|
|
|
|
|
|
speedPercent = Mathf.Clamp01(speed / maxSpeed) * (1f - brakeForce);
|
|
|
|
OnSpeedChange?.Invoke(speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddBrake(float time)
|
|
|
|
{
|
|
|
|
brakeTime = Time.time + time;
|
|
|
|
}
|
|
|
|
|
2023-08-01 11:28:14 +03:00
|
|
|
public void FastBreak(float multiplier)
|
|
|
|
{
|
|
|
|
speed *= multiplier;
|
|
|
|
Debug.Log("fast break");
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:38:13 +03:00
|
|
|
public void RemoveBrake()
|
|
|
|
{
|
|
|
|
brakeTime = 0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddForce(float amount)
|
|
|
|
{
|
|
|
|
addForce = amount;
|
|
|
|
addForce *= RightTrigger.action.ReadValue<float>() * LeftTrigger.action.ReadValue<float>();
|
|
|
|
}
|
|
|
|
}
|