選択している攻撃方法をアイコンで表示する



攻撃アイコンの表示
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 = 4;
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
{
SetAmateras();
}
}
}
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);
}
}



【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 = 4;
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
{
SetAmateras();
}
}
}
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);
}
}



選択している攻撃方法をアイコンで表示する