viewimage.php?id=2abcdd23dad63db0&no=24b0d769e1d32ca73ced82fa11d02831fe384ecd5bf5471a0304a0eb0c94158699bcec718df9416361f33326d3fa7d7a31990f931bd4833dc9112d7735ca07

๋ณด์กฐ๋ฌด๊ธฐ ์œ ๋„๋ฏธ์‚ฌ์ผ ๋งŒ๋“ค์—ˆ์…ˆ


์•„๋ž˜์žˆ๋Š”๊ฑฐ๋Š” ์Šคํฌ๋ฆฝํŠธ์ž„


using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class PlayerHomingBullet : MonoBehaviour

{

GameObject targetEnemy;


void Update()

ย  ย  {

targetEnemy = null;

DetectNearestEnemy();

ChaseEnemy();

}


void DetectNearestEnemy()

{

float tempDist;

float nearestDist = 100;


GameObject[] allEnmemy = GameObject.FindGameObjectsWithTag("Enemy");


for (int i = 0; i

tempDist = Vector3.Distance(allEnmemy[i].transform.position, gameObject.transform.position);

if (tempDist

nearestDist = tempDist;

targetEnemy = allEnmemy[i];

}

}

}


void ChaseEnemy()

{

if (targetEnemy == null) {

transform.position += Vector3.up * 0.1f;

return;

}


Vector3 targetPos = targetEnemy.gameObject.transform.position;

transform.position = Vector3.MoveTowards(transform.position, targetPos, 0.2f);

}

}