Created
January 3, 2022 02:06
-
-
Save curious-username/8bddc2be2963a1e3b2ab3ae1593e10c0 to your computer and use it in GitHub Desktop.
heat seeking requirement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TempMissile : MonoBehaviour | |
{ | |
[SerializeField] | |
private GameObject[] _enemyLocations; | |
private GameObject _closestEnemy; | |
private float _enemyDistance = 1000f; | |
private float _speed = 3.5f; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
_enemyLocations = GameObject.FindGameObjectsWithTag("Enemy"); | |
foreach (GameObject enemy in _enemyLocations) | |
{ | |
float distance = Vector2.Distance(enemy.transform.position, transform.position); | |
if (distance <= _enemyDistance) | |
{ | |
_closestEnemy = enemy; | |
_enemyDistance = distance; | |
} | |
} | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
transform.Translate(Vector2.up * _speed * Time.deltaTime); | |
Vector3 direction = _closestEnemy.transform.position - transform.position; | |
direction.Normalize(); | |
float rot_z = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; | |
transform.rotation = Quaternion.Euler(0f, 0f, rot_z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment