diff --git a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs index eec6601..b740b01 100644 --- a/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs +++ b/Packages/bullet-storm-unity/Scripts/Runtime/BulletSystem/Modules/TracingModule.cs @@ -1,5 +1,6 @@ using System; using CANStudio.BulletStorm.Util; +using NaughtyAttributes; using UnityEngine; #pragma warning disable 0649 @@ -11,10 +12,18 @@ internal struct TracingModule { [Tooltip("Tracing target.")] [SerializeField] private Target target; + [Tooltip("Max rotating angle per second.")] - [Range(0, 180)] + [MinValue(0), AllowNesting] [SerializeField] private float tracingRate; + [SerializeField] + private bool enableRateCurve; + + [Tooltip("x-axis is the angle between bullet's velocity, y-axis is the tracing rate multiplier")] + [CurveRange(0, 0, 180, 1), SerializeField, ShowIf(nameof(enableRateCurve)), AllowNesting] + private AnimationCurve tracingRateCurve; + /// /// Call this on every update. /// @@ -29,13 +38,20 @@ public void OnUpdate(IBulletController bullet) var deltaTime = Time.deltaTime; var targetPosition = target.AsTransform.position; - var ratio = this.tracingRate; - bullet.ChangeVelocity((position, velocity) => - Vector3.RotateTowards( + var rate = tracingRate; + var enableCurve = enableRateCurve; + var curve = tracingRateCurve; + bullet.ChangeVelocity((position, velocity) => + { + var aimDirection = targetPosition - position; + var rateValue = enableCurve ? rate * curve.Evaluate(Vector3.Angle(aimDirection, velocity)) : rate; + if (rateValue < 0) rateValue = 0; + return Vector3.RotateTowards( velocity, - targetPosition - position, - ratio * deltaTime * Mathf.Deg2Rad, - 0)); + aimDirection, + rateValue * deltaTime * Mathf.Deg2Rad, + 0); + }); } } } \ No newline at end of file