¡¡¸½ºß¤Î½èÍý¤Ç¤Ï¡¢
ÃƤοô¤ò¸µ¤Ë¤·¤Æ switch ʸ¤Ë¤è¤ëʬ´ô½èÍý¤ò¹Ô¤Ã¤Æȯ¼ÍÊý¸þ¤ò·×»»¤·¤Æ¤¤¤Þ¤¹¡£
¡¡ÌäÂê¤Ê¤¯Æ°ºî¤·¤Æ¤¤¤Þ¤¹¤¬¡¢²¾¤Ë¡¢ÃƤοô¤òÁý¤ä¤·¤Æ¤¤¤¯¤³¤È¤Ë¤Ê¤ë¤È¡¢¤µ¤é¤Ëʬ´ô½èÍý¤òÄɲ䷤Ƥ¤¤¯É¬Íפ¬¤¢¤ê¡¢ÃƤ¬Áý¤¨¤ë¤Û¤É´ÉÍýÊݼ餬ÂçÊѤˤʤê¤Þ¤¹¡£
¡¡¤½¤Î¤¿¤á¡¢¤³¤Î
switch ʸ¤Ë¤è¤ëʬ´ô½èÍý¤Ë¤Ä¤¤¤Æ¥ê¥Õ¥¡¥¯¥¿¥ê¥ó¥°¤ò¹Ô¤¤¡¢·×»»¼°¤òºîÀ®¤·¤Æ½èÍý¤Î°ì¸µ²½¤ò¿Þ¤ê¤Þ¤¹¡£
¡¡¤É¤¦¤¤¤¦¤³¤È¤«¤È¤¤¤¦¤È¡¢
ÃƤοô¤Ë¹ç¤ï¤»¤Æ¡¢¤É¤Î°ÌÃÖ¤ËÇÛÃÖ¤¹¤ë¤«¤ò¼«Æ°·×»»¤¹¤ë»ÅÁȤߤòºî¤ê¤Þ¤¹¡£
ʬ´ô¤ÎÂå¤ï¤ê¤Ë¼«Æ°·×»»¤µ¤»¤ë¤³¤È¤Ç¡¢ÃƤοô¤Ë±þ¤¸¤¿½èÍý¤ò¼«Æ°²½¤µ¤»¤Þ¤¹¡£
¡¡¤³¤ÎÊýË¡¤Î¥á¥ê¥Ã¥È¤Ï
switch ʸ¤Ë¤è¤ëʬ´ô½èÍý¤¬É¬Íפʤ¯¤Ê¤ë¤³¤È¤Ç¤¹¡£
¤½¤·¤ÆÃƤοô¤¬Áý¤¨¤Æ¤¤¤Ã¤Æ¤â¡¢Ê¬´ô½èÍý¤òÄɲä·¤¿¤ê¡¢½¤Àµ¤¹¤ëɬÍפ¬¤Ê¤¯¤Ê¤ê¤Þ¤¹¡£
¡¡´ÉÍý¤äÊݼ餬³Ú¤Ë¤Ê¤ê¤Þ¤¹¤·¡¢ÃƤοô¤â¼«Í³¤ËÀßÄê¤Ç¤¤Þ¤¹¡£¤Ê¤¼¤Ê¤é¡¢ÃƤοô¤Ë¤è¤ê¡¢È¯¼Í¤¹¤ëÊý¸þ¤¬¼«Æ°²½¤·¤Æ¤¢¤ë¤¿¤á¤Ç¤¹¡£
¡¡¤Þ¤º¤Ï¼«Ê¬¤Îºî¤Ã¤¿Ê¬´ô¤Î½èÍý¤¬¡¢£±¤Ä¤Î·×»»¼°¤È¤·¤Æ¤Þ¤È¤á¤é¤ì¤Ê¤¤¤«¤ò¹Í¤¨¤Æ¤ß¤Þ¤·¤ç¤¦¡£
¤½¤·¤Æ½èÍý¤Î°ì¸µ²½¤¬²Äǽ¤Ç¤¢¤ë¾ì¹ç¡¢½èÍý¤Î¼«Æ°²½¤ò¹Ô¤¦¤³¤È¤Ë¤è¤ê¡¢switch ʸ¤Ë¤è¤ëŤ¤Ê¬´ô½èÍý¤òºî¤é¤Ê¤¯¤ÆºÑ¤à¤è¤¦¤Ë¤Ê¤ê¤Þ¤¹¡£
BulletGenerator.cs
<= ¥¯¥ê¥Ã¥¯¤¹¤ë¤È³«¤¤Þ¤¹¡£
using UnityEngine;
public class BulletGenerator : MonoBehaviour
{
[SerializeField] private Bullet bulletPrefab;
[SerializeField] private CharaController charaController;
[SerializeField] private float offsetDegrees; //³ÑÅ٤δð½àÃÍ(Àð¤Î³ÑÅÙ)
/// <summary>
/// ¥Ð¥ì¥Ã¥ÈÀ¸À®¤Î½àÈ÷¡£À¸À®¤¹¤ëÃƤοô¤Ë¹ç¤ï¤»¤Æʬ´ô¤µ¤»¤ë
/// </summary>
/// <param name="direction"></param>
public void PrepareGenerateBullet(Vector2 direction, int numBullets)
{
////* switch ʸ¤Ë¤è¤ëʬ´ô½èÍý¤ò¥³¥á¥ó¥È¥¢¥¦¥È *////
// switch (numBullets)
// {
// case 1:
// GenerateBullet(direction);
// break;
//
// case 2:
// for (int i = -1; i < 2; i += 2)
// {
// //³ÑÅÙ¤ÎÊäÀµÃͤηèÄê
// float offsetAngle = i * offsetDegrees;
//
// //¾å¤ÎÊäÀµÃͤò²óž¤µ¤»¤¿²óž¾ðÊó¤òºî¤ë(Quaternion.Euler(a, b, c);¤Ç¡¢x¡¢y¡¢z¼´¼þ¤ê¤Ë¤½¤ì¤¾¤ìa¡¢b¡¢cÅٲ󞤹¤ë)
// Quaternion offsetRotation = Quaternion.Euler(0, 0, offsetAngle);
//
// //¾åµ¤Î²óž¾ðÊó¤Ë¡¢direction¤ò³Ý¤±¤ë¤³¤È¤Ç¡¢¤³¤³¤ÇÃƤÎÊý¸þ¤Î¥Ù¥¯¥È¥ë¤¬·è¤Þ¤ë
// Vector2 offsetDirection = offsetRotation * direction;
//
// GenerateBullet(offsetDirection);
//
// //¾å¤Î½èÍý¤ò¤Þ¤È¤á¤ë
// GenerateBullet(CalculateBulletDirection(i, direction));
// }
// break;
//
// case 3:
// for (int i = -1; i < 2; i++)
// {
// GenerateBullet(CalculateBulletDirection(i, direction));
// }
// break;
//
// case 4:
// for (int i = -3; i < 4; i += 2)
// {
// GenerateBullet(CalculateBulletDirection(i, direction));
// }
// break;
//
// default:
// for (int i = -2; i < 3; i++)
// {
// GenerateBullet(CalculateBulletDirection(i, direction));
// }
// break;
// }
////* ¤³¤³¤Þ¤Ç *////
////**¡¡¤³¤³¤«¤é¡¢¿·¤·¤¤½èÍý¤ËÃÖ¤´¹¤¨¤ë¡£·×»»¼°¤Î»ÅÁȤߤòºî¤ë¤³¤È¤Ç¡¢½èÍý¤Î°ì¸µ²½¤ò¿Þ¤ë¡¡**////
// ¾åµ¤Î½èÍý¤ò·×»»¼°¤ËÃÖ¤´¹¤¨¤¿¾ì¹ç
float totalSpreadAngle = offsetDegrees;
for (int i = 0; i < numBullets; i++)
{
// ȯ¼Í³ÑÅÙ¤ò 0 ¤Ç¥»¥Ã¥È(À¸À®¤¹¤ëÃƤ¬£±¤Ä¤Î¤È¤¤Ï¤³¤Î¤Þ¤Þ»È¤¦)
float offsetAngle = 0;
// À¸À®¤¹¤ëÃƤ¬£²¤Ä°Ê¾å¤Î¾ì¹ç
if (numBullets > 1)
{
// ÃƤ餬¤êÁ´ÂΤγÑÅÙ(À𷿤ÎÃæ¿´³Ñ)¤ò¡¢È¯¼Í¤¹¤ëÃƤοô - 1 ¤Ç³ä¤ê¡¢³ÆÃÆ£±¤ÄÅö¤¿¤ê¤Î³ÑÅÙ´Ö³Ö¤ò·×»»¤·¤ÆÊäÀµÃͤȤ¹¤ë
// Î㤨¤Ð totalSpreadAngle = 30 ¤Ê¤é¡¢ÃÆ£²¤Ä¤Î¾ì¹ç¤Ï 30ÅÙ¡¢ÃÆ£³¤Ä¤Î¾ì¹ç¤Ï15Å٤ˤʤë
float angleStep = totalSpreadAngle / (numBullets - 1);
// ³ÆÃƤγÑÅÙ¤ò·×»»¤¹¤ë
// ¤Þ¤º¡¢-totalSpreadAngle / 2 ¤ÏÃƤ餬¤ê¤ÎÃæ¿´¤«¤éº¸Â¦¤Î³ÑÅÙ¤òɽ¤¹
// ¤³¤ÎÃæ¿´¤«¤é¡¢ÃƤοô¤À¤± angleStep ¤òÁý²Ã¤µ¤»¤ë¤³¤È¤Ç¡¢³ÆÃƤγÑÅÙ¤ò·èÄꤹ¤ë
// ¤³¤ÎÁàºî¤Ë¤è¤ê¡¢ÃƤ餬¤ê¤ÎÈÏ°ÏÆâ¤ËÂФ·¤Æ¡¢ÃÆƱ»Î¤Î³ÑÅÙ´Ö³Ö¤òÅù´Ö³Ö(¶ÑÅù)¤ÇÃƤòÇÛÃÖ¤¹¤ë
// Î㤨¤ÐÃÆ£²¤Ä¤Î¾ì¹ç¤Ï -15ÅÙ¤È15ÅÙ(ľ³Ñ»°³Ñ·Á¤ÎÄì³Ñ£²¤Ä¤ÎÊý¸þ)
// ÃÆ£³¤Ä¤Î¾ì¹ç¤Ï -15ÅÙ¡¢0ÅÙ¡¢15ÅÙ(ľ³Ñ»°³Ñ·Á¤Îĺ³Ñ¤ÈÄì³Ñ£²¤Ä¤ÎÊý¸þ)¤Ë¤Ê¤ë
offsetAngle = -totalSpreadAngle / 2 + i * angleStep;
}
Debug.Log($"offsetAngle : { offsetAngle } ");
// Âè1°ú¿ô¤Ï³ÑÅÙ¾ðÊó¤ò¤½¤Î¤Þ¤ÞÅϤ¹
Vector2 offsetDirection = CalculateBulletDirection(offsetAngle, direction);
GenerateBullet(offsetDirection);
}
////* ¤³¤³¤Þ¤Ç *////
}
/// <summary>
/// ¥Ð¥ì¥Ã¥ÈÀ¸À®
/// </summary>
/// <param name="direction"></param>
private void GenerateBullet(Vector2 direction)
{
Bullet bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity); //¤³¤Î¥¯¥é¥¹¤òCharaSet¤Ë¥¢¥¿¥Ã¥Á¤¹¤ì¤Ð¡¢transform.position¤¬¥¥ã¥é¤Î°ÌÃ֤ˤʤë
bullet.transform.SetParent(temporaryObjectsPlace);
bullet.Shoot(direction);
}
/// <summary>
/// ÃƤÎÊý¸þ¤ò·×»»¤¹¤ë
/// </summary>
private Vector2 CalculateBulletDirection(int count, Vector2 direction)
{
//³ÑÅÙ¤ÎÊäÀµÃͤηèÄê
float offsetAngle = count * offsetDegrees;
//¾å¤ÎÊäÀµÃͤò²óž¤µ¤»¤¿²óž¾ðÊó¤òºî¤ë(Quaternion.Euler(a, b, c);¤Ç¡¢x¡¢y¡¢z¼´¼þ¤ê¤Ë¤½¤ì¤¾¤ìa¡¢b¡¢cÅٲ󞤹¤ë)
Quaternion offsetRotation = Quaternion.Euler(0, 0, offsetAngle);
//¾åµ¤Î²óž¾ðÊó¤Ë¡¢direction¤ò³Ý¤±¤ë¤³¤È¤Ç¡¢¤³¤³¤ÇÃƤÎÊý¸þ¤Î¥Ù¥¯¥È¥ë¤¬·è¤Þ¤ë(Unity¤Î¾ì¹ç¡¢Quaternion * Vector3¤ò¤¹¤ë¤È¡¢Vector3¤òQuaternion¤Ç²óž¤µ¤»¤¿ºÂɸ¤¬ÆÀ¤é¤ì¤ë¡£direction¤òoffsetRotation¤À¤±ÊѤ¨¤ë¡£direction¤ÎÊý¸þ¤ò°Ý»ý¤·¤Ä¤Ä¡¢Z¼´¤À¤±¤ò²óž¤µ¤»¤¿¾ðÊó¤ò»ý¤Ã¤Æ¤¤¤ë¿·¤·¤¤¥Ù¥¯¥È¥ë¤òºîÀ®¤·¤Æ¤¤¤ë¡£)
Vector2 offsetDirection = offsetRotation * direction;
return offsetDirection;
}
////* ¿·¤·¤¤¥á¥½¥Ã¥É¤òÄɲ乤ë *////
/// <summary>
/// ÃƤÎÊý¸þ¤ò·×»»¤¹¤ë
/// </summary>
/// <param name="offsetAngle"></param>
/// <param name="direction"></param>
/// <returns></returns>
private Vector2 CalculateBulletDirection(float offsetAngle, Vector2 direction) {
// ÃƤγÑÅÙÊäÀµ¤òɽ¤¹¥¯¥©¡¼¥¿¥Ë¥ª¥ó¤òºîÀ®¤¹¤ë
//¾å¤ÎÊäÀµÃͤò²óž¤µ¤»¤¿²óž¾ðÊó¤òºî¤ë(Quaternion.Euler(a, b, c);¤Ç¡¢x¡¢y¡¢z¼´¼þ¤ê¤Ë¤½¤ì¤¾¤ìa¡¢b¡¢cÅٲ󞤹¤ë)
Quaternion offsetRotation = Quaternion.Euler(0, 0, offsetAngle);
// direction¥Ù¥¯¥È¥ë¤òoffsetRotation¤Ç²óž¤µ¤»¤ë¤³¤È¤Ç¡¢ÃƤο·¤·¤¤Êý¸þ¥Ù¥¯¥È¥ë¤òÆÀ¤ë
// Unity¤ÎQuaternion * Vector3±é»»¤Ë¤è¤ê¡¢Êý¸þ¥Ù¥¯¥È¥ë¤ò²óž¤µ¤»¤¿·ë²Ì¤¬ÆÀ¤é¤ì¤ë
//¾åµ¤Î²óž¾ðÊó¤Ë¡¢direction¤ò³Ý¤±¤ë¤³¤È¤Ç¡¢¤³¤³¤ÇÃƤÎÊý¸þ¤Î¥Ù¥¯¥È¥ë¤¬·è¤Þ¤ë(Unity¤Î¾ì¹ç¡¢Quaternion * Vector3¤ò¤¹¤ë¤È¡¢Vector3¤òQuaternion¤Ç²óž¤µ¤»¤¿ºÂɸ¤¬ÆÀ¤é¤ì¤ë¡£direction¤òoffsetRotation¤À¤±ÊѤ¨¤ë¡£direction¤ÎÊý¸þ¤ò°Ý»ý¤·¤Ä¤Ä¡¢Z¼´¤À¤±¤ò²óž¤µ¤»¤¿¾ðÊó¤ò»ý¤Ã¤Æ¤¤¤ë¿·¤·¤¤¥Ù¥¯¥È¥ë¤òºîÀ®¤·¤Æ¤¤¤ë¡£)
return offsetRotation * direction;
}
////* ¤³¤³¤Þ¤Ç *////
}