티스토리 뷰
게임 매니악스 슈팅 게임 알고리즘에 나오는 조준탄 코드 2-1, 2-2를 Unity에서 테스트해봤습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | using UnityEngine; public class InitAimingBullet : MonoBehaviour { public Transform enemy; public Transform main; public float bulletSpeed; Vector2 mainPos; Vector2 enemyPos; Vector2 bulletPos; Vector2 bulletSpeedVector; float distance; public Vector2 GetBulletSpeedVector() { mainPos = main.transform.position; enemyPos = enemy.transform.position; distance = Vector2.Distance(mainPos, enemyPos); if (distance != 0) { bulletSpeedVector.x = (mainPos.x - enemyPos.x) / distance * bulletSpeed; bulletSpeedVector.y = (mainPos.y - enemyPos.y) / distance * bulletSpeed; return bulletSpeedVector; } else { bulletSpeedVector.x = 0; bulletSpeedVector.y = bulletSpeed; return bulletSpeedVector; } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 | using UnityEngine; public class MoveAimingBullet : MonoBehaviour { public Vector2 bulletSpeedVector; private void Update() { transform.Translate(bulletSpeedVector); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using UnityEngine; public class PlayerMove : MonoBehaviour { public float speed; void Update () { Vector2 v2; v2.x = Input.GetAxis("Horizontal"); v2.y = Input.GetAxis("Vertical"); v2 = v2.normalized; transform.Translate(v2 * speed * Time.deltaTime); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | using UnityEngine; public class EnemyShoot : MonoBehaviour { public GameObject bullet; public InitAimingBullet initAimingBullet; private void Start() { InvokeRepeating("Shoot",0, 5.0f); } void Shoot() { GameObject bullet = Instantiate(this.bullet, transform.position, Quaternion.identity); bullet.GetComponent<MoveAimingBullet>().bulletSpeedVector = initAimingBullet.GetBulletSpeedVector(); } } | cs |
댓글