티스토리 뷰
게임 매니악스 슈팅 게임 알고리즘에 나오는 방향탄 코드 2-7, 2-8을 Unity에서 테스트해봤습니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 | using UnityEngine; public class InitDirectedBullet{ public static Vector2 GetBulletSpeedVector(float theta) { Vector2 bulletSpeedVector; bulletSpeedVector.x = Mathf.Cos(Mathf.PI / 180 * theta); bulletSpeedVector.y = Mathf.Sin(Mathf.PI / 180 * theta); return bulletSpeedVector; } } | cs |
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 | using System.Collections; using UnityEngine; public class EnemyShoot : MonoBehaviour { public GameObject bullet; float theta; private void Start() { StartCoroutine(Shoot()); } IEnumerator Shoot() { yield return null; while(true) { if (theta + 30 >= 360) { theta = 0; } else { theta += 30; } GameObject bullet = Instantiate(this.bullet, transform.position, Quaternion.identity); bullet.GetComponent<MoveDirectedBullet>().bulletSpeedVector = InitDirectedBullet.GetBulletSpeedVector(theta); bullet.GetComponent<MoveDirectedBullet>().speed = 0.1f; yield return new WaitForSeconds(0.1f); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | using UnityEngine; public class MoveDirectedBullet : MonoBehaviour { public Vector2 bulletSpeedVector; public float speed; // Update is called once per frame void Update () { transform.Translate(bulletSpeedVector * speed); } } | cs |
댓글