122 lines
3.3 KiB
C#
122 lines
3.3 KiB
C#
using UnityEngine;
|
|
|
|
public class BoatEngine : MonoBehaviour
|
|
{
|
|
public Boat currentBoat;
|
|
|
|
public float maxTorque = 10f;
|
|
|
|
public float acceleration = 2f;
|
|
|
|
private float currentTorque;
|
|
|
|
private float currentTurning;
|
|
|
|
private float torqueAcceleration;
|
|
|
|
public float currentTorquePercent;
|
|
|
|
public float currentTurningTorquePercent;
|
|
|
|
private AudioSource audioSource;
|
|
|
|
public Transform particlesSplashWater;
|
|
|
|
private void Start()
|
|
{
|
|
audioSource = GetComponent<AudioSource>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
EngineAudioController();
|
|
if ((bool)currentBoat.currentPlayerCharacter)
|
|
{
|
|
if (!particlesSplashWater.gameObject.activeSelf)
|
|
{
|
|
particlesSplashWater.gameObject.SetActive(value: true);
|
|
}
|
|
return;
|
|
}
|
|
if (particlesSplashWater.gameObject.activeSelf)
|
|
{
|
|
particlesSplashWater.gameObject.SetActive(value: false);
|
|
}
|
|
StopEngine();
|
|
}
|
|
|
|
public void StopEngine()
|
|
{
|
|
currentTorque = 0f;
|
|
currentTurning = 0f;
|
|
torqueAcceleration = 0f;
|
|
currentTorquePercent = 0f;
|
|
currentTurningTorquePercent = 0f;
|
|
}
|
|
|
|
public void AddMove(Vector2 input)
|
|
{
|
|
float fixedDeltaTime = Time.fixedDeltaTime;
|
|
if (input.y > 0f)
|
|
{
|
|
currentTorque = Mathf.MoveTowards(currentTorque, 1f, fixedDeltaTime);
|
|
}
|
|
else if (input.y < 0f)
|
|
{
|
|
currentTorque = Mathf.MoveTowards(currentTorque, -1f, fixedDeltaTime);
|
|
}
|
|
if (input.x > 0f)
|
|
{
|
|
currentTurning = Mathf.MoveTowards(currentTurning, 1f, fixedDeltaTime);
|
|
}
|
|
else if (input.x < 0f)
|
|
{
|
|
currentTurning = Mathf.MoveTowards(currentTurning, -1f, fixedDeltaTime);
|
|
}
|
|
else
|
|
{
|
|
currentTurning = Mathf.MoveTowards(currentTurning, 0f, fixedDeltaTime * 0.5f);
|
|
}
|
|
if (input.y == 0f && ((currentTorque > 0f && currentTorque < 0.05f) || (currentTorque < 0f && currentTorque > -0.05f)))
|
|
{
|
|
currentTorque = 0f;
|
|
}
|
|
if (input.x == 0f && ((currentTurning > 0f && currentTurning < 0.1f) || (currentTurning < 0f && currentTurning > -0.1f)))
|
|
{
|
|
currentTurning = 0f;
|
|
}
|
|
currentTorquePercent = currentTorque;
|
|
currentTurningTorquePercent = currentTurning;
|
|
float t = Mathf.Clamp01(currentBoat.rigidbody.velocity.magnitude / 10f);
|
|
torqueAcceleration = Mathf.MoveTowards(torqueAcceleration, currentTorque * maxTorque, fixedDeltaTime * acceleration);
|
|
float num = ((torqueAcceleration != 0f) ? Mathf.Clamp(currentTurning * torqueAcceleration, -50f, 50f) : (currentTurning * 3f));
|
|
float num2 = torqueAcceleration * Mathf.Lerp(1f - Mathf.Abs(currentTurning) * 0.5f, 1f, t);
|
|
float num3 = num * Mathf.Lerp(1f, 0.6f, t);
|
|
currentBoat.rigidbody.AddForce(base.transform.forward * num2);
|
|
currentBoat.rigidbody.AddTorque(currentBoat.transform.up * num3);
|
|
currentBoat.rigidbody.AddTorque(currentBoat.transform.forward * num * 0.5f);
|
|
currentBoat.rigidbody.AddTorque(-currentBoat.transform.right * Mathf.Clamp(currentBoat.rigidbody.velocity.sqrMagnitude, -10f, 50f));
|
|
if ((bool)currentBoat.engineContainer)
|
|
{
|
|
currentBoat.engineContainer.localEulerAngles = new Vector3(0f, 45f * (0f - currentTurning), 0f);
|
|
}
|
|
}
|
|
|
|
private void EngineAudioController()
|
|
{
|
|
if (currentTorquePercent != 0f)
|
|
{
|
|
audioSource.volume = Mathf.Abs(currentTorquePercent) * 0.4f;
|
|
audioSource.pitch = 1f + Mathf.Abs(currentTorquePercent) * 0.1f;
|
|
if (!audioSource.isPlaying)
|
|
{
|
|
audioSource.Play();
|
|
}
|
|
}
|
|
else if (audioSource.isPlaying)
|
|
{
|
|
audioSource.Stop();
|
|
}
|
|
}
|
|
}
|