107 lines
4.0 KiB
C#
107 lines
4.0 KiB
C#
using System;
|
||
using System.Linq;
|
||
using Cielonos.MainGame.UI;
|
||
using FIMSpace.FProceduralAnimation;
|
||
using MoreMountains.FeedbacksForThirdParty;
|
||
using RootMotion.FinalIK;
|
||
using SLSUtilities.General;
|
||
using SLSUtilities.FunctionalAnimation;
|
||
using UnityEngine;
|
||
using UnityEngine.InputSystem;
|
||
|
||
namespace Cielonos.MainGame.Characters
|
||
{
|
||
public partial class PlayerAnimationSubcontroller : AnimationSubcontrollerBase
|
||
{
|
||
public Player player => owner as Player;
|
||
public FunctionalAnimationSubmodule upperBodyFuncAnimSm;
|
||
public AnimatorOverrideController animatorOverride;
|
||
|
||
public FullBodyBipedIK fullBodyBipedIK;
|
||
public GrounderFBBIK grounderFBBIK;
|
||
|
||
public override void Initialize()
|
||
{
|
||
base.Initialize();
|
||
upperBodyFuncAnimSm = new FunctionalAnimationSubmodule(this, "UpperBodyAction");
|
||
player.operationSc.OnDash += (inputDirection, length) => SetupDash(inputDirection, true, length);
|
||
player.operationSc.OnDodge += SetupDodge;
|
||
}
|
||
|
||
protected override void Update()
|
||
{
|
||
base.Update();
|
||
upperBodyFuncAnimSm?.UpdateTime();
|
||
player.inputSc.preinputSubmodule.Update(isDuringPreinput, isAtActionDisruption);
|
||
}
|
||
|
||
protected override void LateUpdate()
|
||
{
|
||
base.LateUpdate();
|
||
upperBodyFuncAnimSm?.UpdateEvents();
|
||
}
|
||
|
||
public override void RegisterDefaultFunctions()
|
||
{
|
||
base.RegisterDefaultFunctions();
|
||
|
||
registeredFunctions.Add("DashStart", anim => DashStart());
|
||
registeredFunctions.Add("DashEnd", anim => DashEnd());
|
||
registeredFunctions.Add("DodgeStart", anim => DodgeStart());
|
||
registeredFunctions.Add("DodgeEnd", anim => DodgeEnd());
|
||
}
|
||
}
|
||
|
||
public partial class PlayerAnimationSubcontroller
|
||
{
|
||
public bool isDuringPreinput;
|
||
public bool isAtActionDisruption;
|
||
|
||
protected override void UpdateIntervalInfo()
|
||
{
|
||
base.UpdateIntervalInfo();
|
||
|
||
isDuringPreinput = currentIntervals.Any(interval => interval.intervalType == IntervalType.Preinput);
|
||
isAtActionDisruption = lastFrameIntervals.SwitchOut(currentIntervals, (interval) => interval.intervalType == IntervalType.Preinput);
|
||
}
|
||
}
|
||
|
||
public partial class PlayerAnimationSubcontroller
|
||
{
|
||
/// <summary>
|
||
/// 计算冲刺时的摄像机倾斜角度
|
||
/// </summary>
|
||
/// <param name="dashDir">冲刺输入的平整化方向 (y=0, normalized)</param>
|
||
/// <param name="camFwd">摄像机的平整化前方 (y=0, normalized)</param>
|
||
/// <returns>Vector3(Pitch角度, 0, Roll角度)</returns>
|
||
public Vector3 CalculateDashAngles(Vector3 dashDir, Vector3 camFwd)
|
||
{
|
||
// 1. 确保输入向量是归一化的(以防万一)
|
||
Vector3 d = dashDir.normalized;
|
||
Vector3 f = camFwd.normalized;
|
||
|
||
// 2. 通过叉乘获取摄像机的水平右方向 (camRight)
|
||
// 在左手坐标系(Unity)中,Up x Forward = Right
|
||
Vector3 r = Vector3.Cross(Vector3.up, f);
|
||
|
||
// 3. 计算投影权重 (范围在 -1 到 1 之间)
|
||
// forwardWeight: 1表示完全同向,-1表示完全反向
|
||
float forwardWeight = Vector3.Dot(d, f);
|
||
// sideWeight: 1表示向右冲,-1表示向左冲
|
||
float sideWeight = Vector3.Dot(d, r);
|
||
|
||
// 4. 定义倾斜强度系数 (控制在 1.5度 左右)
|
||
const float tiltIntensity = 1.5f;
|
||
|
||
// 5. 计算最终角度
|
||
// x 轴旋转 (Pitch):正值向下倾斜(向前冲),负值向上倾斜(向后退)
|
||
float pitch = forwardWeight * tiltIntensity;
|
||
|
||
// z 轴旋转 (Dutch/Roll):
|
||
// 注意:向右冲时(sideWeight=1),通常相机向左倾斜(z为负值)更有动感
|
||
float roll = -sideWeight * tiltIntensity;
|
||
|
||
return new Vector3(pitch, 0, roll);
|
||
}
|
||
}
|
||
} |