ניצחתם
חלק מהיחידה פיזיקה ומשחקיות במסלול ה-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גוף קשיח
כוח הכבידה והמסהדחיפה באמצעות AddForceקפיצה באמצעות מתקףFixedUpdate לפיזיקהמהירותגופים קינמטייםחזרה: משטח שיגור2התנגשויות
OnCollisionEnterבמה פגעתי?להישבר בעת פגיעהלעמוד על הקרקעניתור וחיכוךצורות Colliderסיכום: קיר שביר5פרויקט: מרוץ המטבעות
הכדור המתגלגלמטבעות לאיסוףתרגלו בעצמכם: Unity אונליין