89 lines
3.0 KiB
C#
89 lines
3.0 KiB
C#
using System;
|
||
using UnityEngine;
|
||
using WaveHarmonic.Crest;
|
||
|
||
namespace NBF
|
||
{
|
||
public class Boat : MonoBehaviour
|
||
{
|
||
[Tooltip("移动力应施加于质心的垂直偏移量。")] [SerializeField]
|
||
float _ForceHeightOffset;
|
||
|
||
[Tooltip("水船因推力而移动的速度有多快。")] [SerializeField]
|
||
float _ThrustPower = 10f;
|
||
|
||
[Tooltip("船舵一转,船身就迅速转向。")] [SerializeField]
|
||
float _SteerPower = 1f;
|
||
|
||
[Tooltip("转弯时要转动船只。")] [Range(0, 1)] [SerializeField]
|
||
float _TurningHeel = 0.35f;
|
||
|
||
[Tooltip("对浮力变化应用曲线处理。")] [SerializeField]
|
||
AnimationCurve _BuoyancyCurveFactor = new(new Keyframe[]
|
||
{
|
||
new(0, 0, 0.01267637f, 0.01267637f),
|
||
new(0.6626424f, 0.1791001f, 0.8680198f, 0.8680198f), new(1, 1, 3.38758f, 3.38758f)
|
||
});
|
||
|
||
|
||
/// <summary>
|
||
/// 使用中
|
||
/// </summary>
|
||
public bool IsUse;
|
||
|
||
/// <summary>
|
||
/// 船站立位置
|
||
/// </summary>
|
||
public Transform standPlace;
|
||
|
||
/// <summary>
|
||
/// 浮力组件
|
||
/// </summary>
|
||
private FloatingObject _floatingObject;
|
||
|
||
private float _BuoyancyFactor = 1f;
|
||
private void Awake()
|
||
{
|
||
if (_floatingObject == null) _floatingObject = GetComponent<FloatingObject>();
|
||
}
|
||
|
||
private void FixedUpdate()
|
||
{
|
||
if (!IsUse) return;
|
||
if (!_floatingObject.InWater) return;
|
||
|
||
var input = Vector3.zero;
|
||
Vector2 moveInput = InputManager.GetMovementInput();
|
||
input.x = moveInput.x; // 水平转向(x轴)
|
||
input.z = moveInput.y; // 前后移动(y轴映射到z轴)
|
||
var rb = _floatingObject.RigidBody;
|
||
|
||
// Thrust
|
||
var forcePosition = rb.worldCenterOfMass + _ForceHeightOffset * Vector3.up;
|
||
rb.AddForceAtPosition(_ThrustPower * input.z * transform.forward, forcePosition, ForceMode.Acceleration);
|
||
|
||
// Steer
|
||
var rotation = transform.up + _TurningHeel * transform.forward;
|
||
rb.AddTorque(_SteerPower * input.x * rotation, ForceMode.Acceleration);
|
||
|
||
if (input.y > 0f)
|
||
{
|
||
if (_BuoyancyFactor < 1f)
|
||
{
|
||
_BuoyancyFactor += Time.deltaTime * 0.1f;
|
||
_BuoyancyFactor = Mathf.Clamp(_BuoyancyFactor, 0f, 1f);
|
||
_floatingObject.BuoyancyForceStrength = _BuoyancyCurveFactor.Evaluate(_BuoyancyFactor);
|
||
}
|
||
}
|
||
else if (input.y < 0f)
|
||
{
|
||
if (_BuoyancyFactor > 0f)
|
||
{
|
||
_BuoyancyFactor -= Time.deltaTime * 0.1f;
|
||
_BuoyancyFactor = Mathf.Clamp(_BuoyancyFactor, 0f, 1f);
|
||
_floatingObject.BuoyancyForceStrength = _BuoyancyCurveFactor.Evaluate(_BuoyancyFactor);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
} |