Menu
CoddyTech

사냥꾼의 공격

Coddy Unity 여정의 물리와 게임플레이 섹션에 포함된 레슨. 71개 중 63번째.

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 플레이그라운드