アマテラス(範囲攻撃を行うドローン)の作成


ターゲット範囲攻撃
using UnityEngine;
using Photon.Pun;
public class Amateras : MonoBehaviourPunCallbacks
{
private float timer;
private float interval = 10f;
private GameObject[] targets;
public GameObject shotPoint;
private Vector3 currentTargetPos;
private float launchAngle = 75f;
void Update()
{
if (!photonView.IsMine)
{
return;
}
Vector3 pos = transform.position;
pos.y = 30;
transform.position = Vector3.MoveTowards(transform.position, pos, 0.01f);
timer += Time.deltaTime;
if (timer > interval)
{
targets = GameObject.FindGameObjectsWithTag("Target");
// 一度に攻撃する数の設定
int shotCount = Mathf.Min(targets.Length, 15);
for (int i = 0; i < shotCount; i++)
{
// ターゲットが破壊されていないかを確認
if (targets[i] != null)
{
Shoot(targets[i].transform.position);
}
}
timer = 0;
}
}
void Shoot(Vector3 targetPos)
{
GameObject bomb = PhotonNetwork.Instantiate("Bomb", shotPoint.transform.position, Quaternion.identity);
Rigidbody bombRb = bomb.GetComponent<Rigidbody>();
currentTargetPos = targetPos;
Vector3 diff = currentTargetPos - shotPoint.transform.position;
float h = diff.y;
diff.y = 0;
float distance = diff.magnitude;
float g = Physics.gravity.magnitude;
float angle = launchAngle * Mathf.Deg2Rad;
float cosA = Mathf.Cos(angle);
float tanA = Mathf.Tan(angle);
float denominator = 2 * cosA * cosA * (distance * tanA - h);
if (denominator <= 0)
{
return;
}
float velocity = Mathf.Sqrt((g * distance * distance) / denominator);
Vector3 velocityVec = diff.normalized * velocity * cosA;
velocityVec.y = velocity * Mathf.Sin(angle);
bombRb.linearVelocity = velocityVec;
}
}





【Unity6版】BattleOnline(全38回)
他のコースを見る

ターゲット範囲攻撃
using UnityEngine;
using Photon.Pun;
public class Amateras : MonoBehaviourPunCallbacks
{
private float timer;
private float interval = 10f;
private GameObject[] targets;
public GameObject shotPoint;
private Vector3 currentTargetPos;
private float launchAngle = 75f;
void Update()
{
if (!photonView.IsMine)
{
return;
}
Vector3 pos = transform.position;
pos.y = 30;
transform.position = Vector3.MoveTowards(transform.position, pos, 0.01f);
timer += Time.deltaTime;
if (timer > interval)
{
targets = GameObject.FindGameObjectsWithTag("Target");
// 一度に攻撃する数の設定
int shotCount = Mathf.Min(targets.Length, 15);
for (int i = 0; i < shotCount; i++)
{
// ターゲットが破壊されていないかを確認
if (targets[i] != null)
{
Shoot(targets[i].transform.position);
}
}
timer = 0;
}
}
void Shoot(Vector3 targetPos)
{
GameObject bomb = PhotonNetwork.Instantiate("Bomb", shotPoint.transform.position, Quaternion.identity);
Rigidbody bombRb = bomb.GetComponent<Rigidbody>();
currentTargetPos = targetPos;
Vector3 diff = currentTargetPos - shotPoint.transform.position;
float h = diff.y;
diff.y = 0;
float distance = diff.magnitude;
float g = Physics.gravity.magnitude;
float angle = launchAngle * Mathf.Deg2Rad;
float cosA = Mathf.Cos(angle);
float tanA = Mathf.Tan(angle);
float denominator = 2 * cosA * cosA * (distance * tanA - h);
if (denominator <= 0)
{
return;
}
float velocity = Mathf.Sqrt((g * distance * distance) / denominator);
Vector3 velocityVec = diff.normalized * velocity * cosA;
velocityVec.y = velocity * Mathf.Sin(angle);
bombRb.linearVelocity = velocityVec;
}
}





アマテラス(範囲攻撃を行うドローン)の作成