This commit is contained in:
Ali Sharoz 2025-08-18 22:48:45 +05:00
parent f3dc85b777
commit 700f11bc94
7 changed files with 2541 additions and 128 deletions

View File

@ -24584,7 +24584,7 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 2918029099092491010} - component: {fileID: 2918029099092491010}
m_Layer: 0 m_Layer: 0
m_Name: Obstacles m_Name: ObstaclesFBX
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
@ -30577,7 +30577,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!4 &3211779145275666690 --- !u!4 &3211779145275666690
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -39108,7 +39108,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!4 &4479256775088773384 --- !u!4 &4479256775088773384
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -5023,10 +5023,22 @@ PrefabInstance:
propertyPath: m_Materials.Array.data[0] propertyPath: m_Materials.Array.data[0]
value: value:
objectReference: {fileID: 138099500} objectReference: {fileID: 138099500}
- target: {fileID: 3762423576960139226, guid: aef2a50dc2159e44584497dfc2fdaeb2, type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4441166244158635861, guid: aef2a50dc2159e44584497dfc2fdaeb2, type: 3} - target: {fileID: 4441166244158635861, guid: aef2a50dc2159e44584497dfc2fdaeb2, type: 3}
propertyPath: m_Materials.Array.data[0] propertyPath: m_Materials.Array.data[0]
value: value:
objectReference: {fileID: 1849443317} objectReference: {fileID: 1849443317}
- target: {fileID: 4567716116432550438, guid: aef2a50dc2159e44584497dfc2fdaeb2, type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 5881339069548083274, guid: aef2a50dc2159e44584497dfc2fdaeb2, type: 3}
propertyPath: m_IsActive
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6503432965990616960, guid: aef2a50dc2159e44584497dfc2fdaeb2, type: 3} - target: {fileID: 6503432965990616960, guid: aef2a50dc2159e44584497dfc2fdaeb2, type: 3}
propertyPath: m_Materials.Array.data[0] propertyPath: m_Materials.Array.data[0]
value: value:

File diff suppressed because it is too large Load Diff

View File

@ -9,18 +9,13 @@ public class ChasePlayerController : MonoBehaviour
public float laneDistance = 2.5f; // 0=Left,1=Mid,2=Right public float laneDistance = 2.5f; // 0=Left,1=Mid,2=Right
public float jumpPower = 0.6f; public float jumpPower = 0.6f;
public float jumpDuration = 1.2f; public float jumpDuration = 1.2f;
public float laneSwitchSpeed = 10f; public float laneSwitchSpeed = 10f; // used as units per second
[Header("Animator Driving")] [Header("Animator Driving")]
[Tooltip("If true, sets a float parameter (e.g., 'Speed') each frame. If false, crossfades to a run state by name.")]
public bool useSpeedBlendTree = true; public bool useSpeedBlendTree = true;
[Tooltip("Animator float parameter name for the locomotion blend tree.")]
public string speedParamName = "Speed"; public string speedParamName = "Speed";
[Tooltip("Value to push while running (match your thresholds: 1=Run, 2=RunFast, 3=Sprint).")]
public float runSpeedParamValue = 2f; public float runSpeedParamValue = 2f;
[Tooltip("Fallback run/locomotion state name if not using a Speed parameter.")]
public string runStateName = "Locomotion"; public string runStateName = "Locomotion";
[Tooltip("Base layer index for the states above.")]
public int baseLayer = 0; public int baseLayer = 0;
[Header("Fall / GameOver Logic")] [Header("Fall / GameOver Logic")]
@ -30,33 +25,27 @@ public class ChasePlayerController : MonoBehaviour
public float stateWaitTimeout = 3f; public float stateWaitTimeout = 3f;
public static System.Action<float> OnMoveSpeedChanged; public static System.Action<float> OnMoveSpeedChanged;
// -------------------------------------------------
int currentLane = 1; int currentLane = 1;
Rigidbody rb; Rigidbody rb;
Animator animator; Animator animator;
bool isGrounded = true; bool isGrounded = true;
bool isJumping = false; bool isJumping = false;
Vector3 targetLanePosition;
// touch swipe
Vector2 startTouchPosition; Vector2 startTouchPosition;
bool swipeDetected = false; bool swipeDetected = false;
float minSwipeDistance = 50f; float minSwipeDistance = 50f;
// fall tracking
float lastObstacleHitTime = -999f; float lastObstacleHitTime = -999f;
bool waitingForGameOver = false; bool waitingForGameOver = false;
// state hashes
int runShortHash, fallShortHash, fallingShortHash; int runShortHash, fallShortHash, fallingShortHash;
[SerializeField] bool validateStatesOnStart = true; [SerializeField] bool validateStatesOnStart = true;
[SerializeField] string runTag = "Run"; [SerializeField] string runTag = "Run";
[SerializeField] string fallTag = "Fall"; [SerializeField] string fallTag = "Fall";
[SerializeField] string fallingTag = "Falling"; [SerializeField] string fallingTag = "Falling";
// NEW: remember original forward speed so we can restore after stumble
float originalMoveSpeed; float originalMoveSpeed;
bool unableToMove = false;
ChaseScoreManager scoreManager; ChaseScoreManager scoreManager;
@ -66,9 +55,6 @@ public class ChasePlayerController : MonoBehaviour
animator = GetComponent<Animator>(); animator = GetComponent<Animator>();
scoreManager = FindObjectOfType<ChaseScoreManager>(); scoreManager = FindObjectOfType<ChaseScoreManager>();
targetLanePosition = transform.position;
// cache hashes
runShortHash = Animator.StringToHash(runStateName); runShortHash = Animator.StringToHash(runStateName);
fallShortHash = Animator.StringToHash(fallStateName); fallShortHash = Animator.StringToHash(fallStateName);
fallingShortHash = Animator.StringToHash(fallingStateName); fallingShortHash = Animator.StringToHash(fallingStateName);
@ -76,49 +62,57 @@ public class ChasePlayerController : MonoBehaviour
if (validateStatesOnStart) if (validateStatesOnStart)
{ {
if (!animator.HasState(baseLayer, runShortHash)) if (!animator.HasState(baseLayer, runShortHash))
Debug.LogError($"[ChasePlayerController] Run state '{runStateName}' not found on layer {baseLayer}"); Debug.LogError($"Run state '{runStateName}' not found");
if (!animator.HasState(baseLayer, fallShortHash)) if (!animator.HasState(baseLayer, fallShortHash))
Debug.LogError($"[ChasePlayerController] Fall state '{fallStateName}' not found on layer {baseLayer}"); Debug.LogError($"Fall state '{fallStateName}' not found");
if (!animator.HasState(baseLayer, fallingShortHash)) if (!animator.HasState(baseLayer, fallingShortHash))
Debug.LogError($"[ChasePlayerController] Falling state '{fallingStateName}' not found on layer {baseLayer}"); Debug.LogError($"Falling state '{fallingStateName}' not found");
} }
originalMoveSpeed = moveSpeed; // NEW originalMoveSpeed = moveSpeed;
ForceRunStart(); // autorunner ForceRunStart();
} }
public void SetMoveSpeed(float newSpeed) public void SetMoveSpeed(float newSpeed)
{ {
moveSpeed = newSpeed; moveSpeed = newSpeed;
OnMoveSpeedChanged?.Invoke(newSpeed); OnMoveSpeedChanged?.Invoke(newSpeed);
} }
bool unableToMove = false;
void Update() void Update()
{ {
DriveRunAnimation(); DriveRunAnimation();
if(!unableToMove)
{
HandleInput(); if (!unableToMove)
HandleSwipe(); {
HandleInput();
HandleSwipe();
} }
UpdateLaneTarget();
} }
void FixedUpdate() void FixedUpdate()
{ {
MoveForward(); MoveForward();
SmoothLaneSwitch();
} }
// ----------------- Input -----------------
void HandleInput() void HandleInput()
{ {
if (isJumping) return; if (isJumping) return;
if (Input.GetKeyDown(KeyCode.LeftArrow) && currentLane > 0) currentLane--; if (Input.GetKeyDown(KeyCode.LeftArrow) && currentLane > 0)
if (Input.GetKeyDown(KeyCode.RightArrow) && currentLane < 2) currentLane++; {
if (Input.GetKeyDown(KeyCode.Space) && isGrounded) Jump(); currentLane--;
TweenToLaneX((currentLane - 1) * laneDistance);
}
if (Input.GetKeyDown(KeyCode.RightArrow) && currentLane < 2)
{
currentLane++;
TweenToLaneX((currentLane - 1) * laneDistance);
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
Jump();
} }
void HandleSwipe() void HandleSwipe()
@ -132,43 +126,43 @@ public class ChasePlayerController : MonoBehaviour
startTouchPosition = touch.position; startTouchPosition = touch.position;
swipeDetected = true; swipeDetected = true;
break; break;
case TouchPhase.Ended: case TouchPhase.Ended:
if (!swipeDetected) return; if (!swipeDetected) return;
Vector2 swipe = touch.position - startTouchPosition; Vector2 swipe = touch.position - startTouchPosition;
if (swipe.magnitude >= minSwipeDistance) if (swipe.magnitude >= minSwipeDistance && !isJumping)
{ {
if (isJumping) { swipeDetected = false; return; }
if (Mathf.Abs(swipe.x) > Mathf.Abs(swipe.y)) if (Mathf.Abs(swipe.x) > Mathf.Abs(swipe.y))
{ {
if (swipe.x > 0 && currentLane < 2) currentLane++; if (swipe.x > 0 && currentLane < 2)
else if (swipe.x < 0 && currentLane > 0) currentLane--; {
currentLane++;
TweenToLaneX((currentLane - 1) * laneDistance);
}
else if (swipe.x < 0 && currentLane > 0)
{
currentLane--;
TweenToLaneX((currentLane - 1) * laneDistance);
}
} }
else else if (swipe.y > 0 && isGrounded)
{ {
if (swipe.y > 0 && isGrounded) Jump(); Jump();
} }
} }
swipeDetected = false; swipeDetected = false;
break; break;
} }
} }
// ----------------- Movement ----------------- void TweenToLaneX(float targetX)
void UpdateLaneTarget()
{ {
if (isJumping) return; float distance = Mathf.Abs(rb.position.x - targetX);
float targetX = (currentLane - 1) * laneDistance; float duration = distance / laneSwitchSpeed;
targetLanePosition = new Vector3(targetX, rb.position.y, rb.position.z);
}
void SmoothLaneSwitch() rb.DOMoveX(targetX, duration).SetEase(Ease.OutQuad);
{
if (isJumping) return;
Vector3 newPos = Vector3.Lerp(rb.position, targetLanePosition, laneSwitchSpeed * Time.fixedDeltaTime);
rb.MovePosition(newPos);
} }
void MoveForward() void MoveForward()
@ -189,7 +183,7 @@ public class ChasePlayerController : MonoBehaviour
Vector3 jumpTarget = rb.position + Vector3.back * forwardDisplacement; Vector3 jumpTarget = rb.position + Vector3.back * forwardDisplacement;
rb.DOJump(jumpTarget, jumpPower, 1, jumpDuration) rb.DOJump(jumpTarget, jumpPower, 1, jumpDuration)
.SetEase(Ease.Linear).SetDelay(0.2f) .SetEase(Ease.Linear)
.OnStart(() => .OnStart(() =>
{ {
SafeSetTrigger("Jump"); SafeSetTrigger("Jump");
@ -200,8 +194,7 @@ public class ChasePlayerController : MonoBehaviour
rb.useGravity = true; rb.useGravity = true;
isGrounded = true; isGrounded = true;
isJumping = false; isJumping = false;
SafeSetTrigger("Land");
SafeSetTrigger("Land"); // if present
SafeSetBool("IsGrounded", true); SafeSetBool("IsGrounded", true);
ForceRunStart(); ForceRunStart();
}); });
@ -217,16 +210,10 @@ public class ChasePlayerController : MonoBehaviour
} }
} }
// ----------------- Animator driving -----------------
// CHANGED: can bypass guards when 'ignoreGuards' is true
void ForceRunStart(bool ignoreGuards = false) void ForceRunStart(bool ignoreGuards = false)
{ {
if (!ignoreGuards) if (!ignoreGuards && (IsInOrGoingTo(fallShortHash) || IsInOrGoingTo(fallingShortHash) || waitingForGameOver))
{ return;
if (IsInOrGoingTo(fallShortHash) || IsInOrGoingTo(fallingShortHash) || waitingForGameOver)
return;
}
if (useSpeedBlendTree && HasParameter(speedParamName, AnimatorControllerParameterType.Float)) if (useSpeedBlendTree && HasParameter(speedParamName, AnimatorControllerParameterType.Float))
{ {
@ -245,17 +232,18 @@ public class ChasePlayerController : MonoBehaviour
if (useSpeedBlendTree && HasParameter(speedParamName, AnimatorControllerParameterType.Float)) if (useSpeedBlendTree && HasParameter(speedParamName, AnimatorControllerParameterType.Float))
{ {
animator.SetFloat(speedParamName, runSpeedParamValue, 0.1f, Time.deltaTime); animator.SetFloat(speedParamName, runSpeedParamValue, 0.1f, Time.deltaTime);
return;
} }
else
var st = animator.GetCurrentAnimatorStateInfo(baseLayer);
var nxt = animator.GetNextAnimatorStateInfo(baseLayer);
bool inRunNow = st.shortNameHash == runShortHash || st.IsTag(runTag);
bool goingToRun = animator.IsInTransition(baseLayer) && (nxt.shortNameHash == runShortHash || nxt.IsTag(runTag));
if (!isJumping && !inRunNow && !goingToRun)
{ {
animator.CrossFadeInFixedTime(runStateName, 0.1f, baseLayer, 0f); var st = animator.GetCurrentAnimatorStateInfo(baseLayer);
var nxt = animator.GetNextAnimatorStateInfo(baseLayer);
bool inRunNow = st.shortNameHash == runShortHash || st.IsTag(runTag);
bool goingToRun = animator.IsInTransition(baseLayer) && (nxt.shortNameHash == runShortHash || nxt.IsTag(runTag));
if (!isJumping && !inRunNow && !goingToRun)
{
animator.CrossFadeInFixedTime(runStateName, 0.1f, baseLayer, 0f);
}
} }
} }
@ -271,6 +259,7 @@ public class ChasePlayerController : MonoBehaviour
if (HasParameter(trig, AnimatorControllerParameterType.Trigger)) if (HasParameter(trig, AnimatorControllerParameterType.Trigger))
animator.SetTrigger(trig); animator.SetTrigger(trig);
} }
void SafeSetBool(string param, bool v) void SafeSetBool(string param, bool v)
{ {
if (HasParameter(param, AnimatorControllerParameterType.Bool)) if (HasParameter(param, AnimatorControllerParameterType.Bool))

View File

@ -7,8 +7,8 @@ PhysicMaterial:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_Name: SlipperyFloor m_Name: SlipperyFloor
dynamicFriction: 0 dynamicFriction: 0.1
staticFriction: 0 staticFriction: 0.1
bounciness: 0 bounciness: 0
frictionCombine: 1 frictionCombine: 1
bounceCombine: 1 bounceCombine: 1

View File

@ -78,31 +78,6 @@ AnimatorState:
m_MirrorParameter: m_MirrorParameter:
m_CycleOffsetParameter: m_CycleOffsetParameter:
m_TimeParameter: m_TimeParameter:
--- !u!1101 &-8535053620550059147
AnimatorStateTransition:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_Conditions:
- m_ConditionMode: 1
m_ConditionEvent: Jump
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 6495114904233292258}
m_Solo: 0
m_Mute: 0
m_IsExit: 0
serializedVersion: 3
m_TransitionDuration: 0.25
m_TransitionOffset: 0
m_ExitTime: 0.75
m_HasExitTime: 0
m_HasFixedDuration: 1
m_InterruptionSource: 0
m_OrderedInterruption: 1
m_CanTransitionToSelf: 1
--- !u!1107 &-8077509795911094576 --- !u!1107 &-8077509795911094576
AnimatorStateMachine: AnimatorStateMachine:
serializedVersion: 6 serializedVersion: 6
@ -351,6 +326,31 @@ AnimatorStateTransition:
m_InterruptionSource: 0 m_InterruptionSource: 0
m_OrderedInterruption: 1 m_OrderedInterruption: 1
m_CanTransitionToSelf: 1 m_CanTransitionToSelf: 1
--- !u!1101 &-5813063112835048604
AnimatorStateTransition:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_Conditions:
- m_ConditionMode: 1
m_ConditionEvent: Jump
m_EventTreshold: 0
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 702539109886631529}
m_Solo: 0
m_Mute: 0
m_IsExit: 0
serializedVersion: 3
m_TransitionDuration: 0.25
m_TransitionOffset: 0
m_ExitTime: 0.75
m_HasExitTime: 0
m_HasFixedDuration: 1
m_InterruptionSource: 0
m_OrderedInterruption: 1
m_CanTransitionToSelf: 1
--- !u!1101 &-5312677403108563446 --- !u!1101 &-5312677403108563446
AnimatorStateTransition: AnimatorStateTransition:
m_ObjectHideFlags: 1 m_ObjectHideFlags: 1
@ -967,7 +967,7 @@ AnimatorStateMachine:
m_StateMachine: {fileID: -7106055725313943096} m_StateMachine: {fileID: -7106055725313943096}
m_Position: {x: 0, y: 0, z: 0} m_Position: {x: 0, y: 0, z: 0}
m_AnyStateTransitions: m_AnyStateTransitions:
- {fileID: -8535053620550059147} - {fileID: -5813063112835048604}
m_EntryTransitions: [] m_EntryTransitions: []
m_StateMachineTransitions: {} m_StateMachineTransitions: {}
m_StateMachineBehaviours: [] m_StateMachineBehaviours: []
@ -1010,7 +1010,7 @@ AnimatorState:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_Name: Locomotion m_Name: Locomotion
m_Speed: 200 m_Speed: 100
m_CycleOffset: 0 m_CycleOffset: 0
m_Transitions: m_Transitions:
- {fileID: 6179490260077758370} - {fileID: 6179490260077758370}
@ -1967,10 +1967,10 @@ AnimatorStateMachine:
m_ChildStates: m_ChildStates:
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: 6495114904233292258} m_State: {fileID: 6495114904233292258}
m_Position: {x: 200, y: 0, z: 0} m_Position: {x: 360, y: -20, z: 0}
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: 702539109886631529} m_State: {fileID: 702539109886631529}
m_Position: {x: 235, y: 65, z: 0} m_Position: {x: 260, y: 40, z: 0}
- serializedVersion: 1 - serializedVersion: 1
m_State: {fileID: -2656865062802190136} m_State: {fileID: -2656865062802190136}
m_Position: {x: 270, y: 130, z: 0} m_Position: {x: 270, y: 130, z: 0}

View File

@ -32,10 +32,10 @@ EditorBuildSettings:
- enabled: 0 - enabled: 0
path: Assets/Scenes/FeedTheZibu.unity path: Assets/Scenes/FeedTheZibu.unity
guid: 2ccc400ec771453428e1f25a49906737 guid: 2ccc400ec771453428e1f25a49906737
- enabled: 1 - enabled: 0
path: Assets/Scenes/ChaseRun.unity path: Assets/Scenes/ChaseRun.unity
guid: be6c423b3d68dcb48bc49a7d2ed4957d guid: be6c423b3d68dcb48bc49a7d2ed4957d
- enabled: 0 - enabled: 1
path: Assets/Scenes/CrateEscape.unity path: Assets/Scenes/CrateEscape.unity
guid: af5c5d2a2d201e24f8c4913ae531addf guid: af5c5d2a2d201e24f8c4913ae531addf
m_configObjects: {} m_configObjects: {}