Added threat speed up

This commit is contained in:
Hazim Bin Ijaz 2025-09-04 17:01:33 +05:00
parent 817edd2938
commit fadffbc004
3 changed files with 57 additions and 3 deletions

View File

@ -2105,6 +2105,11 @@ PrefabInstance:
insertIndex: -1
addedObject: {fileID: 1319432447}
m_SourcePrefab: {fileID: 100100000, guid: f82ba3fc97fa2e941990becbb5eca05b, type: 3}
--- !u!4 &1319432443 stripped
Transform:
m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: f82ba3fc97fa2e941990becbb5eca05b, type: 3}
m_PrefabInstance: {fileID: 1319432442}
m_PrefabAsset: {fileID: 0}
--- !u!1 &1319432444 stripped
GameObject:
m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: f82ba3fc97fa2e941990becbb5eca05b, type: 3}
@ -2147,6 +2152,7 @@ MonoBehaviour:
rb: {fileID: 1319432448}
moveSpeed: 1
stopDistance: 1
usePlayerSpeed: 0
--- !u!65 &1319432447
BoxCollider:
m_ObjectHideFlags: 0
@ -2525,6 +2531,11 @@ MonoBehaviour:
fallingStateName: Falling
secondHitWindow: 10
stateWaitTimeout: 3
speedUpFromBomb: 1
bombEnemy: {fileID: 0}
boostDistance: 12
maxSpeedBoost: 4
boostLerp: 5
waitingForGameOver: 0
validateStatesOnStart: 1
runTag: Run
@ -8855,6 +8866,14 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6341515697253773609, guid: 515cc30092d66fe4dba290f9d6415a60, type: 3}
propertyPath: bombEnemy
value:
objectReference: {fileID: 1319432443}
- target: {fileID: 6341515697253773609, guid: 515cc30092d66fe4dba290f9d6415a60, type: 3}
propertyPath: maxSpeedBoost
value: 2.5
objectReference: {fileID: 0}
- target: {fileID: 6341515697253773609, guid: 515cc30092d66fe4dba290f9d6415a60, type: 3}
propertyPath: initialJumpVelocity
value: 4

View File

@ -55,6 +55,16 @@ public class ChasePlayerController : MonoBehaviour
[SerializeField] private float secondHitWindow = 10f;
[SerializeField] private float stateWaitTimeout = 3f;
[Header("Threat-Based Speedup")]
[SerializeField] private bool speedUpFromBomb = true;
[SerializeField] private Transform bombEnemy; // assign in Inspector or auto-find by tag "BombEnemy"
[SerializeField] private float boostDistance = 12f; // within this range -> up to max boost
[SerializeField] private float maxSpeedBoost = 4f; // extra speed added at zero distance
[SerializeField] private float boostLerp = 5f; // smoothing (higher = snappier)
private float baseMoveSpeed; // remembers your normal run speed
public static System.Action<float> OnMoveSpeedChanged;
// ----- runtime state -----
@ -110,7 +120,12 @@ public class ChasePlayerController : MonoBehaviour
runShortHash = Animator.StringToHash(runStateName);
fallShortHash = Animator.StringToHash(fallStateName);
fallingShortHash = Animator.StringToHash(fallingStateName);
if (bombEnemy == null)
{
var bomb = GameObject.FindGameObjectWithTag("BombEnemy");
if (bomb) bombEnemy = bomb.transform;
}
// Validate states (optional)
if (validateStatesOnStart)
{
@ -150,8 +165,24 @@ public class ChasePlayerController : MonoBehaviour
ApplyBetterJumpGravity();
ClampVerticalSpeed();
ApplyThreatSpeedup();
}
private void ApplyThreatSpeedup()
{
if (!speedUpFromBomb || bombEnemy == null || waitingForGameOver) return;
float dist = Vector3.Distance(transform.position, bombEnemy.position);
// 0 when far, 1 when enemy is on top of us
float t = Mathf.Clamp01((boostDistance - dist) / boostDistance);
float target = baseMoveSpeed + maxSpeedBoost * t;
// Smoothly approach the target; no event fire here so enemy doesn't copy this boost
moveSpeed = Mathf.Lerp(moveSpeed, target, Time.deltaTime * boostLerp);
}
private void FixedUpdate()
{
MoveForwardAndSide();

View File

@ -10,6 +10,8 @@ public class ChaseRunEnemy : MonoBehaviour
private Transform player;
private float stopDistanceSqr;
private bool isStopped = false;
[Header("Speed Source")]
public bool usePlayerSpeed = false; // set to false so enemy won't copy the wolf's boost
private void Awake()
{
@ -30,12 +32,14 @@ public class ChaseRunEnemy : MonoBehaviour
private void OnEnable()
{
ChasePlayerController.OnMoveSpeedChanged += UpdateMoveSpeed;
if (usePlayerSpeed)
ChasePlayerController.OnMoveSpeedChanged += UpdateMoveSpeed;
}
private void OnDisable()
{
ChasePlayerController.OnMoveSpeedChanged -= UpdateMoveSpeed;
if (usePlayerSpeed)
ChasePlayerController.OnMoveSpeedChanged += UpdateMoveSpeed;
}
private void UpdateMoveSpeed(float speed)