修改脚本

This commit is contained in:
2026-03-22 09:33:56 +08:00
parent 1e1fec02cd
commit ba5da38dde
127 changed files with 31514 additions and 22327 deletions

View File

@@ -18,6 +18,7 @@ namespace NBF
public PlayerArm LeftArm;
public PlayerArm RightArm;
public Transform Pinch;
public Transform RodRoot
{

View File

@@ -86,7 +86,7 @@ public class SceneSettings : MonoBehaviour
private void UpdateTimeOfDay()
{
var p = GameTimer.GetGameDayProgress();
// p = 0;
p = 0.5f;
// Debug.Log(p);
EnviroManager.instance.Time.SetTimeOfDay(p * 24f);
// if(AzureCoreSystem)

View File

@@ -0,0 +1,23 @@
using System;
using UnityEngine;
namespace NBF
{
public class SetTransform : MonoBehaviour
{
public Vector3 targetPos = new Vector3(0, 0, 0);
public Vector3 targetAngles = new Vector3(0, 0, 0);
public void Start()
{
targetPos = transform.localPosition;
targetAngles = transform.localEulerAngles;
}
private void LateUpdate()
{
transform.localPosition = targetPos;
transform.localEulerAngles = targetAngles;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a38b7c15293e4a559f5ab9110a089d4c
timeCreated: 1774091684

View File

@@ -37,7 +37,7 @@ namespace NBF
}
}
private float _lineLength = 4.5f;
private float _lineLength = 4.2f;
/// <summary>
/// 线长度

View File

@@ -0,0 +1,85 @@
using UnityEngine;
namespace NBF
{
public class JointPinchController : MonoBehaviour
{
// 配置参数
[SerializeField] private float moveSpeed = 5f;
[SerializeField] private float snapDistance = 0.1f;
// 组件引用
private ConfigurableJoint originalSpringJoint;
private FixedJoint pinchJoint;
private Rigidbody rb;
private Transform targetTransform;
private float originalSpring;
public bool isPinched { get; private set; }
private bool moveToTargetDone;
private float _speed;
void Start()
{
rb = GetComponent<Rigidbody>();
originalSpringJoint = GetComponent<ConfigurableJoint>();
}
void FixedUpdate()
{
if (isPinched && targetTransform != null)
{
transform.position =
Vector3.MoveTowards(transform.position, targetTransform.position, Time.deltaTime * _speed);
if (!moveToTargetDone)
{
if (Vector3.Distance(transform.position, targetTransform.position) < 0.1f)
{
moveToTargetDone = true;
}
}
if (moveToTargetDone)
{
transform.position = targetTransform.position;
}
}
}
// 外部调用:开始捏住流程
public void StartPinch(Transform fingerTransform, float speed = 3)
{
_speed = speed;
Rigidbody fingerRb = fingerTransform.GetComponent<Rigidbody>();
if (fingerRb == null)
{
Debug.LogError("目标必须带有Rigidbody");
return;
}
isPinched = true;
rb.useGravity = false;
rb.isKinematic = true;
moveToTargetDone = false;
targetTransform = fingerTransform;
}
// 外部调用:释放捏住
public void ReleasePinch()
{
isPinched = false;
rb.useGravity = true;
rb.linearVelocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
rb.isKinematic = false;
rb.linearVelocity = Vector3.zero;
rb.angularVelocity = Vector3.zero;
targetTransform = null;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1de1bec90e454664a860c5248170ff95
timeCreated: 1773588197

View File

@@ -152,6 +152,7 @@ namespace NBF
/// </summary>
public void OnRodThrowStart()
{
Debug.LogError("OnRodThrowStart");
if (Player.State == PlayerState.Throw)
{
var playerStateView = Player.GetComponent<PlayerStateView>();
@@ -167,6 +168,7 @@ namespace NBF
/// </summary>
public void OnRodThrownEnd()
{
Debug.LogError("OnRodThrownEnd");
if (Player.State == PlayerState.Throw)
{
var playerStateView = Player.GetComponent<PlayerStateView>();

View File

@@ -84,6 +84,12 @@ namespace NBF
Unity.ModelAsset.PlayerAnimator.OnUseItem(itemView);
}
}
var stateView = Player.GetComponent<PlayerStateView>();
if (stateView != null && stateView.CurrentStateView is PlayerStageViewIdle playerStageViewIdle)
{
playerStageViewIdle.TakeLine();
}
Player.IsChangeItemIng = false;
}

View File

@@ -0,0 +1,230 @@
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using System;
namespace NBF
{
/// <summary>
/// 抛竿输入参数
/// </summary>
[Serializable]
public struct ThrowInput
{
[Range(0, 1)] public float power; // 力度 0-1
[Range(5, 50)] public float lureWeight; // 钓饵重量 g
[Range(0.1f, 1f)] public float lineDiameter; // 线直径 mm
public Vector3 windDirection;
public float windStrength;
}
/// <summary>
/// 路亚抛竿轨迹控制器
/// 使用预计算轨迹 + DOTween动画实现确定性抛竿
/// </summary>
public class LureThrowTrajectory : IDisposable
{
// 物理常量
private const float AirDensity = 1.225f;
// 默认参数
protected float _minThrowPower = 15f;
protected float _maxThrowPower = 45f;
private float _dragCoefficient = 0.2f;
private float _lureArea = 0.001f;
private float _timeStep = 0.02f;
private int _maxSteps = 500;
private Sequence _throwSequence;
private List<Vector3> _trajectory;
public bool IsPlaying => _throwSequence != null && _throwSequence.IsPlaying();
public List<Vector3> Trajectory => _trajectory;
/// <summary>
/// 计算抛竿轨迹
/// </summary>
public List<Vector3> CalculateTrajectory(ThrowInput input, Vector3 startPos, Vector3 throwDirection)
{
_trajectory = new List<Vector3>();
Vector3 position = startPos;
Vector3 direction = throwDirection.normalized;
float throwPower = Mathf.Lerp(_minThrowPower, _maxThrowPower, input.power);
Vector3 velocity = direction * throwPower;
float lureMass = input.lureWeight / 1000f;
Vector3 windDir = input.windDirection.normalized;
float windStrength = input.windStrength;
float currentTime = 0f;
int steps = 0;
while (currentTime < 10f && steps < _maxSteps)
{
if (position.y <= 0f) break;
// 风力影响
float windInfluenceFactor = Mathf.Clamp01(currentTime / 1.5f);
Vector3 windVelocity = windDir * windStrength * windInfluenceFactor;
Vector3 relVelocity = velocity - windVelocity;
// 空气阻力
float dragMag = 0.5f * AirDensity * relVelocity.sqrMagnitude * _dragCoefficient * _lureArea;
// 钓线阻力
float lineLength = Vector3.Distance(
new Vector3(position.x, 0, position.z),
new Vector3(startPos.x, 0, startPos.z));
float lineRadius = input.lineDiameter / 2000f;
float lineArea = lineLength * (lineRadius * 2f);
float lineDragMag = 0.5f * AirDensity * velocity.sqrMagnitude * _dragCoefficient * lineArea;
Vector3 lineDragForce = -velocity.normalized * lineDragMag;
Vector3 dragForce = -relVelocity.normalized * dragMag;
Vector3 totalForce = dragForce + lineDragForce;
Vector3 acceleration = Physics.gravity + totalForce / lureMass;
velocity += acceleration * _timeStep;
position += velocity * _timeStep;
_trajectory.Add(position);
currentTime += _timeStep;
steps++;
}
return _trajectory;
}
/// <summary>
/// 执行抛竿动画
/// </summary>
public void ExecuteThrow(
Transform lureTransform,
Rigidbody lureRigidbody,
List<Vector3> trajectory,
float duration,
Action onComplete = null)
{
if (trajectory == null || trajectory.Count < 2)
{
onComplete?.Invoke();
return;
}
// 停止之前的动画
Kill();
// 设置为运动学模式
lureRigidbody.isKinematic = true;
lureRigidbody.useGravity = false;
// 创建路径动画
_throwSequence = DOTween.Sequence();
// 使用 DOPath 沿路径移动
var pathArray = trajectory.ToArray();
_throwSequence.Append(
lureTransform.DOPath(pathArray, duration, PathType.Linear, PathMode.Full3D)
.SetEase(Ease.Linear)
.SetLookAt(0.01f) // 让Lure朝向运动方向
);
_throwSequence.OnComplete(() =>
{
// 动画结束,恢复物理
lureRigidbody.isKinematic = false;
lureRigidbody.useGravity = true;
lureRigidbody.linearVelocity = Vector3.zero;
lureRigidbody.angularVelocity = Vector3.zero;
onComplete?.Invoke();
});
}
/// <summary>
/// 计算合适的飞行时间(基于轨迹长度)
/// </summary>
public float CalculateDuration(List<Vector3> trajectory, float speedFactor = 1f)
{
if (trajectory == null || trajectory.Count < 2) return 1f;
float totalLength = 0f;
for (int i = 1; i < trajectory.Count; i++)
{
totalLength += Vector3.Distance(trajectory[i - 1], trajectory[i]);
}
// 根据轨迹长度计算时间,越长越慢
return Mathf.Clamp(totalLength / (20f * speedFactor), 0.5f, 3f);
}
/// <summary>
/// 使用简化轨迹(减少点位,优化性能)
/// </summary>
public List<Vector3> SimplifyTrajectory(List<Vector3> points, float tolerance = 0.1f)
{
if (points == null || points.Count < 3) return new List<Vector3>(points ?? new List<Vector3>());
var result = new List<Vector3>();
SimplifySection(points, 0, points.Count - 1, tolerance, result);
result.Add(points[points.Count - 1]);
return result;
}
private void SimplifySection(List<Vector3> points, int start, int end, float tolerance, List<Vector3> result)
{
if (end <= start + 1) return;
float maxDistance = -1f;
int index = -1;
for (int i = start + 1; i < end; i++)
{
float distance = PerpendicularDistance(points[i], points[start], points[end]);
if (distance > maxDistance)
{
maxDistance = distance;
index = i;
}
}
if (maxDistance > tolerance)
{
SimplifySection(points, start, index, tolerance, result);
result.Add(points[index]);
SimplifySection(points, index, end, tolerance, result);
}
}
private float PerpendicularDistance(Vector3 point, Vector3 lineStart, Vector3 lineEnd)
{
if (lineStart == lineEnd) return Vector3.Distance(point, lineStart);
Vector3 projected = Vector3.Project(point - lineStart, lineEnd - lineStart);
return Vector3.Distance(point, lineStart + projected);
}
public void Kill()
{
if (_throwSequence != null)
{
_throwSequence.Kill();
_throwSequence = null;
}
}
public void Dispose()
{
Kill();
_trajectory?.Clear();
}
/// <summary>
/// 设置抛竿力度范围
/// </summary>
public void SetPowerRange(float min, float max)
{
_minThrowPower = min;
_maxThrowPower = max;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: cc9b0a5776333e54d9b96a7f6104891e

View File

@@ -32,7 +32,7 @@ namespace NBF
/// </summary>
Fight = 5
}
public abstract class PlayerStageViewBase
{
public Player Player { get; private set; }
@@ -89,5 +89,54 @@ namespace NBF
protected virtual void OnUpdate()
{
}
public void TakeLine()
{
if (Player == null) return;
var view = Player.GetComponent<PlayerView>();
if (view != null)
{
var handItemView = Player.HandItem.GetComponent<PlayerItemView>();
if (handItemView != null && handItemView.Rod != null)
{
if (handItemView.Rod.Line.PinchController != null)
{
handItemView.Rod.Line.PinchController.StartPinch(view.Unity.ModelAsset.Pinch);
}
}
}
}
public void UnTakeLine()
{
if (Player == null) return;
var view = Player.GetComponent<PlayerView>();
if (view != null)
{
var handItemView = Player.HandItem.GetComponent<PlayerItemView>();
if (handItemView != null && handItemView.Rod != null)
{
if (handItemView.Rod.Line.PinchController != null)
{
handItemView.Rod.Line.PinchController.ReleasePinch();
}
}
}
}
public FRod GetRod()
{
var view = Player.GetComponent<PlayerView>();
if (view != null)
{
var handItemView = Player.HandItem.GetComponent<PlayerItemView>();
if (handItemView != null && handItemView.Rod != null)
{
return handItemView.Rod;
}
}
return null;
}
}
}

View File

@@ -59,9 +59,9 @@ namespace NBF
addNum *= 0.5f;
}
// Debug.Log($"addNum={addNum}");
PlayerView.Unity.ModelAsset.PlayerAnimator.FishingUp += addNum;
// Debug.LogError($"ishingFinal={Player.ModelAsset.PlayerAnimator.FishingUp}");
Debug.Log($"addNum={addNum} ishingFinal={PlayerView.Unity.ModelAsset.PlayerAnimator.FishingUp}");
// Debug.LogError($"ishingFinal={PlayerView.Unity.ModelAsset.PlayerAnimator.FishingUp}");
if (PlayerView.Unity.ModelAsset.PlayerAnimator.FishingUp >= 1)
{
PlayerView.Unity.ModelAsset.PlayerAnimator.FishingUp = 1;

View File

@@ -5,6 +5,7 @@
protected override void OnEnter()
{
InputManager.OnOp1Action += OnOp1Action;
TakeLine();
}
private void OnOp1Action(bool performed)
@@ -21,5 +22,6 @@
{
InputManager.OnOp1Action -= OnOp1Action;
}
}
}

View File

@@ -26,6 +26,7 @@ namespace NBF
protected override void OnEnter()
{
Log.Info("enter PlayerStatePrepare");
ChargedProgress = 0;
Stage = Phase.Charged;
PlayerView.Unity.ModelAsset.PlayerAnimator.PrepareThrow = true;
PlayerView.Unity.ModelAsset.PlayerAnimator.FishingUp = 0;
@@ -43,7 +44,7 @@ namespace NBF
Debug.Log($"确认蓄力结果,ChargedProgress={ChargedProgress}");
var par = new StateEnterParams();
par.SetFloat(StateParamsConst.ChargedProgress, ChargedProgress);
Player.ChangeState(PlayerState.Throw);
Player.ChangeState(PlayerState.Throw, par);
Stage = Phase.None;
}
}
@@ -55,7 +56,8 @@ namespace NBF
{
if (ChargedProgress < 1)
{
ChargedProgress += Time.deltaTime;
ChargedProgress += Time.deltaTime * 0.5f;
Debug.Log($"ChargedProgress={ChargedProgress}");
}
else if (ChargedProgress > 1)
{

View File

@@ -8,6 +8,14 @@ namespace NBF
private bool _nextState = false;
public float ChargedProgress;
[Header("抛竿参数")]
[SerializeField] private float throwUpAngle = 25f; // 向上抛出的角度(度)
[SerializeField] private float throwDuration = 1.5f; // 抛竿飞行时间
[SerializeField] private float minThrowPower = 15f;
[SerializeField] private float maxThrowPower = 60f;
private LureThrowTrajectory _trajectoryController;
protected override void OnEnter()
{
Log.Info("enter PlayerStateThrow");
@@ -21,9 +29,17 @@ namespace NBF
Debug.Log($"PlayerThrow ChargedProgress={ChargedProgress}");
_nextState = false;
// Stage = Phase.Waiting;
// _owner.Gears.Reel?.Unlock();
// 初始化轨迹控制器
_trajectoryController?.Dispose();
_trajectoryController = new LureThrowTrajectory();
_trajectoryController.SetPowerRange(minThrowPower, maxThrowPower);
}
protected override void OnExit()
{
// 清理轨迹控制器
_trajectoryController?.Kill();
}
protected override void OnUpdate()
@@ -31,26 +47,11 @@ namespace NBF
CheckStateTimeout(10);
if (_nextState)
{
// return (uint)PlayerState.Fishing;
_nextState = false;
Player.ChangeState(PlayerState.Fishing);
}
}
// IEnumerator ThrowCoroutine(float distance)
// {
// float startLength = 0.5f;
// Debug.Log($"REST LENGTH : {rope.restLength}");
// do
// {
// float a = Vector3.Distance(rodTipTarget.position, attachedBody.transform.position);
// attachedBody.RBody.AddForce(playerForward.Value, ForceMode.VelocityChange);
// startLength = Mathf.Max(a, startLength);
// UnwindLine(attachedBody.RBody.linearVelocity.magnitude * Time.deltaTime);
// yield return null;
// }
// while ((bool)isBailOpen);
// }
#region
@@ -60,16 +61,79 @@ namespace NBF
public void OnRodThrowStart()
{
Debug.LogError("OnRodThrowStart");
UnTakeLine();
PlayerView.Unity.ModelAsset.PlayerAnimator.PrepareThrow = false;
PlayerView.Unity.ModelAsset.PlayerAnimator.StartThrow = false;
var rod = GetRod();
if (rod != null && rod.Line != null && rod.Line.Lure != null)
{
var lureTransform = rod.Line.Lure.transform;
var lureRigidbody = rod.Line.Lure.RBody;
// 计算抛竿起点(竿尖位置)
Vector3 startPos = rod.Asset.lineConnector.position;
// 计算抛竿方向(基于角色朝向 + 向上角度)
float radians = throwUpAngle * Mathf.Deg2Rad;
Vector3 forward = PlayerView.Unity.transform.forward;
Vector3 throwDirection = forward * Mathf.Cos(radians) + Vector3.up * Mathf.Sin(radians);
// 构建抛竿输入参数
var throwInput = new ThrowInput
{
power = ChargedProgress,
lureWeight = 10f, // TODO: 从钓饵配置获取
lineDiameter = 0.2f, // TODO: 从线配置获取
windDirection = Vector3.zero,
windStrength = 0f
};
// 计算轨迹
var trajectory = _trajectoryController.CalculateTrajectory(throwInput, startPos, throwDirection);
// 简化轨迹(优化性能)
trajectory = _trajectoryController.SimplifyTrajectory(trajectory, 0.15f);
// 计算飞行时间(基于轨迹长度)
float duration = _trajectoryController.CalculateDuration(trajectory, 0.8f + ChargedProgress * 0.5f);
// 先将Lure瞬移到起点
lureTransform.position = startPos;
// 执行轨迹动画
_trajectoryController.ExecuteThrow(
lureTransform,
lureRigidbody,
trajectory,
duration,
() =>
{
// 动画完成,切换状态
_nextState = true;
}
);
Debug.Log($"抛竿轨迹: 点位数={trajectory.Count}, 飞行时间={duration:F2}s, 落点={trajectory[^1]}");
}
}
public void OnRodThrownEnd()
{
Debug.LogError("OnRodThrownEnd");
_nextState = true;
// 状态切换现在由轨迹动画完成回调处理
}
#endregion
}
// 扩展 LureThrowTrajectory 添加 SetPowerRange 方法
public partial class LureThrowTrajectory
{
public void SetPowerRange(float min, float max)
{
_minThrowPower = min;
_maxThrowPower = max;
}
}
}

View File

@@ -27,6 +27,7 @@ namespace NBF
public LureController Lure;
public BobberController Bobber;
public JointPinchController PinchController;
// public event Action OnLinePulled;