@@ -49,10 +49,26 @@ public class LapCounter : MonoBehaviour
49
49
50
50
void Start ( )
51
51
{
52
- playerCar . SetActive ( false ) ; // 确保玩家车在开始时不激活
53
- aiCar . SetActive ( false ) ; // 确保 AI 警车在开始时不激活
54
- StartCoroutine ( StartRace ( ) ) ; // 启动协程处理倒计时和激活逻辑
55
- aiCar . SetActive ( true ) ; // 确保 AI 警车在开始时不激活
52
+ Rigidbody PlayercarRigidbody = playerCar . GetComponent < Rigidbody > ( ) ;
53
+ if ( PlayercarRigidbody != null )
54
+ {
55
+ PlayercarRigidbody . isKinematic = true ; // 禁用物理运动
56
+ // 或者使用 constraints
57
+ // carRigidbody.constraints = RigidbodyConstraints.FreezeAll;
58
+ } if ( aiCar != null )
59
+ {
60
+ AICarPathFollower aiCarPathFollower = aiCar . GetComponent < AICarPathFollower > ( ) ;
61
+ aiCarPathFollower . enabled = false ; // 确保 AI 小车的路径跟随脚本启用
62
+ Rigidbody AicarRigidbody = aiCar . GetComponent < Rigidbody > ( ) ;
63
+ if ( AicarRigidbody != null )
64
+ {
65
+ AicarRigidbody . isKinematic = true ; // 禁用物理运动
66
+ // 或者使用 constraints
67
+ // carRigidbody.constraints = RigidbodyConstraints.FreezeAll;
68
+ }
69
+ }
70
+ StartCoroutine ( StartRace ( PlayercarRigidbody , aiCar ) ) ; // 启动协程处理倒计时和激活逻辑
71
+
56
72
UpdateLapDisplay ( ) ;
57
73
UpdateTimeDisplay ( ) ;
58
74
UpdateSpeedDisplay ( ) ;
@@ -83,14 +99,27 @@ void Start()
83
99
Debug . LogWarning ( "AI car is not assigned in LapCounter!" ) ;
84
100
}
85
101
}
86
- IEnumerator StartRace ( )
102
+ IEnumerator StartRace ( Rigidbody playerCar , GameObject aiCar )
87
103
{
88
104
countdownscript . StartCountdown ( 5 ) ; // 开始5秒倒计时
89
105
yield return new WaitForSeconds ( 5.0f ) ; // 等待倒计时结束
90
- playerCar . SetActive ( true ) ; // 激活玩家车
106
+ playerCar . isKinematic = false ; // 激活玩家小车的物理运动
91
107
Debug . Log ( "玩家小车已激活!" ) ;
92
108
yield return new WaitForSeconds ( 3.0f ) ; // 再等待5秒
93
- aiCar . SetActive ( true ) ; // 激活 AI 小车
109
+ if ( aiCar != null )
110
+ {
111
+ AICarPathFollower aiCarPathFollower = aiCar . GetComponent < AICarPathFollower > ( ) ;
112
+ Rigidbody AicarRigidbody = aiCar . GetComponent < Rigidbody > ( ) ;
113
+ if ( aiCarPathFollower != null )
114
+ {
115
+ aiCarPathFollower . enabled = true ; // 确保 AI 小车的路径跟随脚本启用
116
+ AicarRigidbody . isKinematic = false ; // 激活 AI 小车的物理运动
117
+ }
118
+ else
119
+ {
120
+ Debug . LogWarning ( "AI car does not have AICarPathFollower component!" ) ;
121
+ }
122
+ }
94
123
Debug . Log ( "AI 小车已激活!" ) ;
95
124
}
96
125
void Update ( )
0 commit comments