Menu
CoddyTech

נשיכת הציידים

חלק מהיחידה פיזיקה ומשחקיות במסלול ה-Unity של Coddy. שיעור 63 מתוך 71.

challenge icon

אתגר

בינוני

גרום לציידים לנשוך ולהדוף את השחקן לאחור.

  1. ב־Hunter, הוסף [SerializeField] private float knockback = 12f; ואת void OnCollisionEnter(Collision collision).
  2. בתוך הפונקציה, כאשר collision.gameObject.TryGetComponent(out Health health) מתקיים, קרא ל־health.TakeDamage(1);.
  3. לאחר מכן דחוף אותו: away מהצייד אל השחקן, עם y ששווה ל־0, ועם collision.rigidbody.AddForce(away.normalized * knockback, ForceMode.Impulse);.

לחץ על "שחק" וראה כמה זמן תחזיק מעמד. לאחר מכן לחץ על "בדיקה".

נסו בעצמכם

using UnityEngine;

public class Hunter : MonoBehaviour
{
    [SerializeField] private float speed = 3f;
    private Rigidbody rb;
    private Transform player;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        player = GameObject.FindWithTag("Player").transform;
    }

    void FixedUpdate()
    {
        Vector3 toPlayer = player.position - rb.position;
        toPlayer.y = 0f;
        Vector3 dir = toPlayer.normalized;
        rb.linearVelocity = new Vector3(dir.x * speed, rb.linearVelocity.y, dir.z * speed);
    }
}

כל השיעורים ביחידה פיזיקה ומשחקיות

תרגלו בעצמכם: Unity אונליין