프로젝트/게임
[아르마딜로 대시] 4. 유니티 원형 탄막 이동
유(YOO)
2021. 1. 6. 16:40
BulletFire 스크립트에 원형 탄막 개수가 들어있음(총 8개로 지정함)
=> 변수를 사용하기 위함이므로 생략 가능
BulletFire 스크립트 whyou-story.tistory.com/17
파란색은 새로 생성되는 탄막들을 한 오브젝트 하위에서 관리하기 위하여 생성하였고,
빨간색은 작은 탄막들이 모인 원형 유닛(bulletUnit)
노란색은 작은 탄막 하나하나 오브젝트이다.(bulletUnit의 child)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// 유닛 단위
public class BulletMove : MonoBehaviour
{
BulletFire bulletFire; // 탄막 생성 스크립트
public GameObject bulletUnit; // 탄막 유닛
float speed = 3f; // 속도
void Start()
{
bulletFire = GameObject.Find("FollowingMouse").GetComponent<BulletFire>();
}
void Update()
{
if(bulletUnit.CompareTag("BulletUnit0"))
{
float angle = 360 / bulletFire.circleBulletNum; // 각도
try
{
// 원형 작은 탄막 발사
for (int i = 1; i < bulletFire.circleBulletNum + 1; i++)
{
bulletUnit.transform.GetChild(i).gameObject.GetComponent<Rigidbody2D>().velocity
= new Vector2(speed * Mathf.Cos(Mathf.PI * 2 * i / bulletFire.circleBulletNum), speed * Mathf.Sin(Mathf.PI * 2 * i / bulletFire.circleBulletNum));
bulletUnit.transform.GetChild(i).gameObject.transform.Rotate(new Vector3(0f, 0f, angle * i - 90));
}
}
catch (Exception e) { }
}
}
}
처음에는 Addforce를 사용하려 했지만, 일정한 힘을 계속 가해 등속 운동이 안되므로 velocity를 변경하는 방법을 사용하였다.
원형 탄막을 생성하기 위하여 cos과 sin 함수를 이용한다.(x축이 cos, y축이 sin)
try-catch문은 탄막 생성 전에 child가 탄막 개수만큼 생성되지 않은 에러를 방지한다.