BeamShotの追加(ターゲットに向かって直線軌道のビーム弾を発射する)






ビーム弾の実装
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
public class AttackController : MonoBehaviourPunCallbacks
{
public GameObject shotPoint;
private float throwForce;
private float throwAngle;
private GameObject targetMark;
private Vector3 currentTargetPos;
private float launchAngle = 75f;
private int num = 0;
// ★改良
private int attacks = 5;
public AudioClip changeSound;
public Sprite[] attackImages;
private Image attackIcon;
void Start()
{
if (!photonView.IsMine)
{
return;
}
targetMark = GameObject.Find("TargetMark");
attackIcon = GameObject.Find("AttackIcon").GetComponent<Image>();
attackIcon.sprite = attackImages[num];
}
void Update()
{
if (!photonView.IsMine)
{
return;
}
if (InputManager.isa.Player.AttackChange.triggered)
{
num = (num + 1) % attacks;
AudioSource.PlayClipAtPoint(changeSound, Camera.main.transform.position);
attackIcon.sprite = attackImages[num];
}
if (InputManager.isa.Player.Throw.triggered)
{
// ★改良
if (num == 0) ThrowBomb();
else if (num == 1) Targetshoot();
else if (num == 2) SetDrone();
else if (num == 3) SetAmateras();
else if (num == 4) BeamShot(); // ★追加
}
}
void ThrowBomb()
{
throwForce = 10f;
throwAngle = 45f;
GameObject bomb = PhotonNetwork.Instantiate("Bomb", shotPoint.transform.position, Quaternion.identity);
Rigidbody bombRb = bomb.GetComponent<Rigidbody>();
Vector3 forceDirection = Quaternion.AngleAxis(-throwAngle, transform.right) * transform.forward;
bombRb.AddForce(forceDirection * throwForce, ForceMode.VelocityChange);
}
void Targetshoot()
{
Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, 0);
Ray ray = Camera.main.ScreenPointToRay(screenCenter);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
currentTargetPos = hit.point;
targetMark.transform.position = hit.point;
Shoot();
}
}
void Shoot()
{
GameObject bomb = PhotonNetwork.Instantiate("Bomb", shotPoint.transform.position, Quaternion.identity);
Rigidbody bombRb = bomb.GetComponent<Rigidbody>();
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;
}
void SetDrone()
{
PhotonNetwork.Instantiate("Drone", shotPoint.transform.position, Quaternion.identity);
}
void SetAmateras()
{
PhotonNetwork.Instantiate("Amateras", shotPoint.transform.position, Quaternion.identity);
}
// ★追加
void BeamShot()
{
// 画面中央からレイを飛ばして着弾点を決める
Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, 0);
Ray ray = Camera.main.ScreenPointToRay(screenCenter);
RaycastHit hit;
// デフォルトの着弾点は「正面100m先」にしておく(何にも当たらなかった時用)
Vector3 targetPoint = ray.origin + (ray.direction * 100f);
if (Physics.Raycast(ray, out hit))
{
targetPoint = hit.point;
targetMark.transform.position = hit.point;
}
// ビーム弾の生成
GameObject beam = PhotonNetwork.Instantiate("Beam", shotPoint.transform.position, Quaternion.identity);
// 弾をターゲットの方向に向かせる
beam.transform.LookAt(targetPoint);
// 物理挙動で直進させる
Rigidbody beamRb = beam.GetComponent<Rigidbody>();
if (beamRb != null)
{
float beamSpeed = 50f;
// 重力オフ
beamRb.useGravity = false;
// ターゲットへの進行方向を計算
Vector3 direction = (targetPoint - shotPoint.transform.position).normalized;
// ビーム発射
beamRb.AddForce(direction * beamSpeed, ForceMode.VelocityChange);
}
}
}

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





ビーム弾の実装
using UnityEngine;
using Photon.Pun;
using UnityEngine.UI;
public class AttackController : MonoBehaviourPunCallbacks
{
public GameObject shotPoint;
private float throwForce;
private float throwAngle;
private GameObject targetMark;
private Vector3 currentTargetPos;
private float launchAngle = 75f;
private int num = 0;
// ★改良
private int attacks = 5;
public AudioClip changeSound;
public Sprite[] attackImages;
private Image attackIcon;
void Start()
{
if (!photonView.IsMine)
{
return;
}
targetMark = GameObject.Find("TargetMark");
attackIcon = GameObject.Find("AttackIcon").GetComponent<Image>();
attackIcon.sprite = attackImages[num];
}
void Update()
{
if (!photonView.IsMine)
{
return;
}
if (InputManager.isa.Player.AttackChange.triggered)
{
num = (num + 1) % attacks;
AudioSource.PlayClipAtPoint(changeSound, Camera.main.transform.position);
attackIcon.sprite = attackImages[num];
}
if (InputManager.isa.Player.Throw.triggered)
{
// ★改良
if (num == 0) ThrowBomb();
else if (num == 1) Targetshoot();
else if (num == 2) SetDrone();
else if (num == 3) SetAmateras();
else if (num == 4) BeamShot(); // ★追加
}
}
void ThrowBomb()
{
throwForce = 10f;
throwAngle = 45f;
GameObject bomb = PhotonNetwork.Instantiate("Bomb", shotPoint.transform.position, Quaternion.identity);
Rigidbody bombRb = bomb.GetComponent<Rigidbody>();
Vector3 forceDirection = Quaternion.AngleAxis(-throwAngle, transform.right) * transform.forward;
bombRb.AddForce(forceDirection * throwForce, ForceMode.VelocityChange);
}
void Targetshoot()
{
Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, 0);
Ray ray = Camera.main.ScreenPointToRay(screenCenter);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
currentTargetPos = hit.point;
targetMark.transform.position = hit.point;
Shoot();
}
}
void Shoot()
{
GameObject bomb = PhotonNetwork.Instantiate("Bomb", shotPoint.transform.position, Quaternion.identity);
Rigidbody bombRb = bomb.GetComponent<Rigidbody>();
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;
}
void SetDrone()
{
PhotonNetwork.Instantiate("Drone", shotPoint.transform.position, Quaternion.identity);
}
void SetAmateras()
{
PhotonNetwork.Instantiate("Amateras", shotPoint.transform.position, Quaternion.identity);
}
// ★追加
void BeamShot()
{
// 画面中央からレイを飛ばして着弾点を決める
Vector3 screenCenter = new Vector3(Screen.width / 2, Screen.height / 2, 0);
Ray ray = Camera.main.ScreenPointToRay(screenCenter);
RaycastHit hit;
// デフォルトの着弾点は「正面100m先」にしておく(何にも当たらなかった時用)
Vector3 targetPoint = ray.origin + (ray.direction * 100f);
if (Physics.Raycast(ray, out hit))
{
targetPoint = hit.point;
targetMark.transform.position = hit.point;
}
// ビーム弾の生成
GameObject beam = PhotonNetwork.Instantiate("Beam", shotPoint.transform.position, Quaternion.identity);
// 弾をターゲットの方向に向かせる
beam.transform.LookAt(targetPoint);
// 物理挙動で直進させる
Rigidbody beamRb = beam.GetComponent<Rigidbody>();
if (beamRb != null)
{
float beamSpeed = 50f;
// 重力オフ
beamRb.useGravity = false;
// ターゲットへの進行方向を計算
Vector3 direction = (targetPoint - shotPoint.transform.position).normalized;
// ビーム発射
beamRb.AddForce(direction * beamSpeed, ForceMode.VelocityChange);
}
}
}

BeamShotの追加(ターゲットに向かって直線軌道のビーム弾を発射する)