Bombを自動発射するドローンの作成




Bombの自動発射
using UnityEngine;
using Photon.Pun;
public class Drone : MonoBehaviourPunCallbacks
{
public GameObject shotPoint;
private float shotRange = 20f;
private float shotInterval = 1f;
private float timer;
void Update()
{
if (!photonView.IsMine)
{
return;
}
// 回転
transform.Rotate(Vector3.up * Time.deltaTime * 60);
// 攻撃
CheckAndShot();
}
void CheckAndShot()
{
timer += Time.deltaTime;
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, shotRange))
{
if (timer >= shotInterval)
{
// Bomb発射
ShotBomb();
timer = 0;
}
}
}
void ShotBomb()
{
GameObject bomb = PhotonNetwork.Instantiate("Bomb", shotPoint.transform.position, Quaternion.identity);
Rigidbody bombRb = bomb.GetComponent<Rigidbody>();
bombRb.AddForce(transform.forward * 2000);
}
}

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



Bombの自動発射
using UnityEngine;
using Photon.Pun;
public class Drone : MonoBehaviourPunCallbacks
{
public GameObject shotPoint;
private float shotRange = 20f;
private float shotInterval = 1f;
private float timer;
void Update()
{
if (!photonView.IsMine)
{
return;
}
// 回転
transform.Rotate(Vector3.up * Time.deltaTime * 60);
// 攻撃
CheckAndShot();
}
void CheckAndShot()
{
timer += Time.deltaTime;
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, shotRange))
{
if (timer >= shotInterval)
{
// Bomb発射
ShotBomb();
timer = 0;
}
}
}
void ShotBomb()
{
GameObject bomb = PhotonNetwork.Instantiate("Bomb", shotPoint.transform.position, Quaternion.identity);
Rigidbody bombRb = bomb.GetComponent<Rigidbody>();
bombRb.AddForce(transform.forward * 2000);
}
}

Bombを自動発射するドローンの作成