쫑가 과정

적 AI. 플레이어 따라가기. 본문

Unity2D/구현

적 AI. 플레이어 따라가기.

쫑가 2022. 2. 3. 20:44
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody2D))]
public class Enemy : MonoBehaviour
{
    [SerializeField] protected float moveSpeed;
    [SerializeField] protected Transform player;

    private protected Rigidbody2D rb;
    private protected Vector2 movement;
  
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class EnemyYul : Enemy
{
    
    private void Start()
    {
        rb = this.GetComponent<Rigidbody2D>();
    }
    private void Update()
    {
        Vector3 direction = player.position - transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        rb.rotation = angle;
        direction.Normalize();
        movement = direction;
    }
    private void FixedUpdate()
    {
        MoveCharacter(movement);
    }
    private void MoveCharacter(Vector2 direction)
    {
        rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    }
}

'Unity2D > 구현' 카테고리의 다른 글

2D sorting / 2d 오브젝트 렌더러 우선순위 정하기.  (1) 2022.02.16
2d 클릭 2) 더블 클릭  (0) 2022.01.26
2d 클릭 1) 오브젝트 클릭  (0) 2022.01.24
Comments