Files
2026-03-04 09:37:33 +08:00

141 lines
6.7 KiB
C#

using DG.Tweening.Core;
using DG.Tweening.Core.Enums;
using DG.Tweening.Plugins;
using DG.Tweening.Plugins.Core.PathCore;
using DG.Tweening.Plugins.Options;
using UnityEngine;
namespace DG.Tweening
{
public static class DOTweenModulePhysics
{
public static TweenerCore<Vector3, Vector3, VectorOptions> DOMove(this Rigidbody target, Vector3 endValue, float duration, bool snapping = false)
{
TweenerCore<Vector3, Vector3, VectorOptions> tweenerCore = DOTween.To(() => target.position, target.MovePosition, endValue, duration);
tweenerCore.SetOptions(snapping).SetTarget(target);
return tweenerCore;
}
public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveX(this Rigidbody target, float endValue, float duration, bool snapping = false)
{
TweenerCore<Vector3, Vector3, VectorOptions> tweenerCore = DOTween.To(() => target.position, target.MovePosition, new Vector3(endValue, 0f, 0f), duration);
tweenerCore.SetOptions(AxisConstraint.X, snapping).SetTarget(target);
return tweenerCore;
}
public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveY(this Rigidbody target, float endValue, float duration, bool snapping = false)
{
TweenerCore<Vector3, Vector3, VectorOptions> tweenerCore = DOTween.To(() => target.position, target.MovePosition, new Vector3(0f, endValue, 0f), duration);
tweenerCore.SetOptions(AxisConstraint.Y, snapping).SetTarget(target);
return tweenerCore;
}
public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveZ(this Rigidbody target, float endValue, float duration, bool snapping = false)
{
TweenerCore<Vector3, Vector3, VectorOptions> tweenerCore = DOTween.To(() => target.position, target.MovePosition, new Vector3(0f, 0f, endValue), duration);
tweenerCore.SetOptions(AxisConstraint.Z, snapping).SetTarget(target);
return tweenerCore;
}
public static TweenerCore<Quaternion, Vector3, QuaternionOptions> DORotate(this Rigidbody target, Vector3 endValue, float duration, RotateMode mode = RotateMode.Fast)
{
TweenerCore<Quaternion, Vector3, QuaternionOptions> tweenerCore = DOTween.To(() => target.rotation, target.MoveRotation, endValue, duration);
tweenerCore.SetTarget(target);
tweenerCore.plugOptions.rotateMode = mode;
return tweenerCore;
}
public static TweenerCore<Quaternion, Vector3, QuaternionOptions> DOLookAt(this Rigidbody target, Vector3 towards, float duration, AxisConstraint axisConstraint = AxisConstraint.None, Vector3? up = null)
{
TweenerCore<Quaternion, Vector3, QuaternionOptions> tweenerCore = DOTween.To(() => target.rotation, target.MoveRotation, towards, duration).SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetLookAt);
tweenerCore.plugOptions.axisConstraint = axisConstraint;
tweenerCore.plugOptions.up = ((!up.HasValue) ? Vector3.up : up.Value);
return tweenerCore;
}
public static Sequence DOJump(this Rigidbody target, Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
{
if (numJumps < 1)
{
numJumps = 1;
}
float startPosY = 0f;
float offsetY = -1f;
bool offsetYSet = false;
Sequence s = DOTween.Sequence();
Tween yTween = DOTween.To(() => target.position, target.MovePosition, new Vector3(0f, jumpPower, 0f), duration / (float)(numJumps * 2)).SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad)
.SetRelative()
.SetLoops(numJumps * 2, LoopType.Yoyo)
.OnStart(delegate
{
startPosY = target.position.y;
});
s.Append(DOTween.To(() => target.position, target.MovePosition, new Vector3(endValue.x, 0f, 0f), duration).SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)).Join(DOTween.To(() => target.position, target.MovePosition, new Vector3(0f, 0f, endValue.z), duration).SetOptions(AxisConstraint.Z, snapping).SetEase(Ease.Linear)).Join(yTween)
.SetTarget(target)
.SetEase(DOTween.defaultEaseType);
yTween.OnUpdate(delegate
{
if (!offsetYSet)
{
offsetYSet = true;
offsetY = (s.isRelative ? endValue.y : (endValue.y - startPosY));
}
Vector3 position = target.position;
position.y += DOVirtual.EasedValue(0f, offsetY, yTween.ElapsedPercentage(), Ease.OutQuad);
target.MovePosition(position);
});
return s;
}
public static TweenerCore<Vector3, Path, PathOptions> DOPath(this Rigidbody target, Vector3[] path, float duration, PathType pathType = PathType.Linear, PathMode pathMode = PathMode.Full3D, int resolution = 10, Color? gizmoColor = null)
{
if (resolution < 1)
{
resolution = 1;
}
TweenerCore<Vector3, Path, PathOptions> tweenerCore = DOTween.To(PathPlugin.Get(), () => target.position, target.MovePosition, new Path(pathType, path, resolution, gizmoColor), duration).SetTarget(target).SetUpdate(UpdateType.Fixed);
tweenerCore.plugOptions.isRigidbody = true;
tweenerCore.plugOptions.mode = pathMode;
return tweenerCore;
}
public static TweenerCore<Vector3, Path, PathOptions> DOLocalPath(this Rigidbody target, Vector3[] path, float duration, PathType pathType = PathType.Linear, PathMode pathMode = PathMode.Full3D, int resolution = 10, Color? gizmoColor = null)
{
if (resolution < 1)
{
resolution = 1;
}
Transform trans = target.transform;
TweenerCore<Vector3, Path, PathOptions> tweenerCore = DOTween.To(PathPlugin.Get(), () => trans.localPosition, delegate(Vector3 x)
{
target.MovePosition((trans.parent == null) ? x : trans.parent.TransformPoint(x));
}, new Path(pathType, path, resolution, gizmoColor), duration).SetTarget(target).SetUpdate(UpdateType.Fixed);
tweenerCore.plugOptions.isRigidbody = true;
tweenerCore.plugOptions.mode = pathMode;
tweenerCore.plugOptions.useLocalPosition = true;
return tweenerCore;
}
internal static TweenerCore<Vector3, Path, PathOptions> DOPath(this Rigidbody target, Path path, float duration, PathMode pathMode = PathMode.Full3D)
{
TweenerCore<Vector3, Path, PathOptions> tweenerCore = DOTween.To(PathPlugin.Get(), () => target.position, target.MovePosition, path, duration).SetTarget(target);
tweenerCore.plugOptions.isRigidbody = true;
tweenerCore.plugOptions.mode = pathMode;
return tweenerCore;
}
internal static TweenerCore<Vector3, Path, PathOptions> DOLocalPath(this Rigidbody target, Path path, float duration, PathMode pathMode = PathMode.Full3D)
{
Transform trans = target.transform;
TweenerCore<Vector3, Path, PathOptions> tweenerCore = DOTween.To(PathPlugin.Get(), () => trans.localPosition, delegate(Vector3 x)
{
target.MovePosition((trans.parent == null) ? x : trans.parent.TransformPoint(x));
}, path, duration).SetTarget(target);
tweenerCore.plugOptions.isRigidbody = true;
tweenerCore.plugOptions.mode = pathMode;
tweenerCore.plugOptions.useLocalPosition = true;
return tweenerCore;
}
}
}