لقد فزت
جزء من قسم الفيزياء وأسلوب اللعب في رحلة Unity على Coddy. الدرس 33 من 71.
التحدي
متوسطاجعل اللاعب يفوز عند الوصول إلى target نقطة.
- في
Ball.cs، أضفpublic int target = 10;وbool IsPlaying()، بحيث تُعيدtimeLeft > 0f && score < target. - استخدم
IsPlaying()بدلًا منtimeLeft > 0fفيUpdate()وFixedUpdate()، وابدأAddScoreباستخدامif (!IsPlaying()) return;. - بعد سطر النتيجة، عندما يكون
score >= target، سجّلYou win!.
اضغط «العب» واجمع 8 نقاط. ثم اضغط «تحقق».
جرّب بنفسك
using UnityEngine;
public class Ball : MonoBehaviour
{
public float force = 10f;
public int score;
public float timeLimit = 60f;
public float timeLeft;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
timeLeft = timeLimit;
}
void Update()
{
if (timeLeft > 0f)
{
timeLeft -= Time.deltaTime;
if (timeLeft <= 0f)
{
timeLeft = 0f;
Debug.Log("Time's up! Score: " + score);
}
}
}
void FixedUpdate()
{
if (timeLeft > 0f)
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
rb.AddForce(new Vector3(x, 0f, z) * force);
}
}
public void AddScore(int points)
{
score += points;
Debug.Log("Score: " + score);
}
}
جميع دروس الفيزياء وأسلوب اللعب
1الجسم الصلب (Rigidbody)
الجاذبية والكتلةالدفع باستخدام AddForceالقفز باستخدام دفعةFixedUpdate للفيزياءالسرعة المتجهةالأجسام الحركيةمراجعة: منصة الإطلاق4الكوروتينات
InvokeInvokeRepeating, CancelInvokeأول Coroutine لكالحلقات داخل الكوروتيناتإيقاف الكوروتيناتمراجعة: العد التنازلي10المشروع: الهروب من الحلبة
الحلبةالصيادونتدرّب بنفسك: Playground لـ Unity