티스토리 뷰
게임클라이언트 프로그래밍/R&D
Bullet Pattern R&D (포물선(parabola) 형태의 bullet)
Game Client Lee Hwanguk 2023. 3. 13. 17:05https://www.youtube.com/watch?v=711KSrbTIt4
https://www.youtube.com/watch?v=ddakS7BgHRI
https://www.youtube.com/watch?v=EZnIXz6GLlE
https://www.youtube.com/watch?v=Qxs3GrhcZI8
#enemy의 기준으로 Random.insideUnitCircle.normalized 사용했으나 너무 범위가 넓어 위와 같은 모습이 나오지 않았다
#내각을 정해주고 그 위치에서 흩뿌려주게 만들어야했다
using UnityEngine;
public class BossBulletPattern : MonoBehaviour
{
public GameObject bulletPrefab; // 총알 프리팹
public int numBullets = 5; // 총알 개수
public float spreadAngle = 30f; // 총알이 퍼지는 각도 범위
public float bulletSpeed = 5f; // 총알 속도
public float bulletLifetime = 3f; // 총알 지속 시간
private Vector2 pointL;
private Vector2 pointR;
private void Start()
{
//내각
var pivot = 90;
var sight = 45;
var cos = Mathf.Cos((pivot + sight) * Mathf.Deg2Rad);
var sin=Mathf.Sin((pivot+sight)*Mathf.Deg2Rad);
this.pointL=new Vector3(cos,0,sin);
var cos2 = Mathf.Cos((pivot - sight) * Mathf.Deg2Rad);
var sin2 = Mathf.Sin((pivot - sight) * Mathf.Deg2Rad);
this.pointR = new Vector3(cos2, 0, sin2);
//0f 후 2f간격으로 "Fire"호출
InvokeRepeating("Fire", 0f, 2f);
}
private void Fire()
{
Debug.LogFormat("<color=red>{0}, {1}</color>",this.pointL,this.pointR); //-0.71, 0.71
// 총알을 여러 방향으로 뿌림
for (int i = 0; i < numBullets; i++)
{
// 랜덤한 방향과 속도로 총알을 발사
//insideUnitCircle->random point inside a circle with radius 1 (원호 안에서 랜덤한 값 반환)
//Vector2 direction = Random.insideUnitCircle.normalized; //너무 범위가 넓음, 내각을 정해줄 필요가 있음
Vector2 direction=this.pointR-this.pointL; //방향
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
//float angle = Mathf.Atan2(direction.y, direction.x)/2 * Mathf.Rad2Deg;
angle += Random.Range(-spreadAngle, spreadAngle);
direction = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right;
GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
// 일정 시간이 지나면 총알을 파괴
Destroy(bullet, bulletLifetime);
}
}
}
using UnityEngine;
public class BossBulletPattern : MonoBehaviour
{
public GameObject bulletPrefab; // 총알 프리팹
public int numBullets = 5; // 총알 개수
public float spreadAngle = 30f; // 총알이 퍼지는 각도 범위
public float bulletSpeed = 5f; // 총알 속도
public float bulletLifetime = 3f; // 총알 지속 시간
public Vector2 pointL;
public Vector2 pointR;
private void Start()
{
//내각
var pivot = 180;
var sight = 90;
var cos = Mathf.Cos((pivot + sight) * Mathf.Deg2Rad);
var sin=Mathf.Sin((pivot+sight)*Mathf.Deg2Rad);
//this.pointL=new Vector3(cos,0,sin);
this.pointL=new Vector2(cos,sin);
var cos2 = Mathf.Cos((pivot - sight) * Mathf.Deg2Rad);
var sin2 = Mathf.Sin((pivot - sight) * Mathf.Deg2Rad);
//this.pointR = new Vector3(cos2,0,sin2);
this.pointR=new Vector2(cos2,sin2);
//0f 후 2f간격으로 "Fire"호출
InvokeRepeating("Fire", 0f, 2f);
}
private void Fire()
{
//Debug.LogFormat("<color=red>{0}, {1}</color>",this.pointL,this.pointR); //-0.71, 0.71
// 총알을 여러 방향으로 뿌림
for (int i = 0; i < numBullets; i++)
{
// 랜덤한 방향과 속도로 총알을 발사
//insideUnitCircle->random point inside a circle with radius 1 (원호 안에서 랜덤한 값 반환)
//Vector2 direction = Random.insideUnitCircle.normalized; //너무 범위가 넓음, 내각을 정해줄 필요가 있음
Vector2 direction=this.pointR-this.pointL; //방향
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
//float angle = Mathf.Atan2(direction.y, direction.x)/2 * Mathf.Rad2Deg;
angle += Random.Range(-spreadAngle, spreadAngle);
direction = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right;
GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
bullet.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
// 일정 시간이 지나면 총알을 파괴
Destroy(bullet, bulletLifetime);
}
}
}
#bullet의 lifetime이 지나면 사라지며 그 위치에범위공격 효과를 구현하고싶다
'게임클라이언트 프로그래밍 > R&D' 카테고리의 다른 글
Bullet pattern(test tool) (1) | 2023.03.20 |
---|---|
Bullet Pattern R&D (Laser,LineRenderer를 이용한 공격 패턴) (0) | 2023.03.15 |
Bullet Pattern R&D (Player를 따라오는 유도탄'homing missail' bullet) (0) | 2023.03.13 |
Bullet Pattern R&D (Player를 기준으로 360도 Bullet 생성) (0) | 2023.03.11 |
Bullet Pattern R&D (Player의 90~270 방향으로 Bullet 생성) (0) | 2023.03.11 |