アマテラスの設置


アマテラスの設置
using UnityEngine;
using Photon.Pun;
public class AttackController : MonoBehaviourPunCallbacks
{
public GameObject shotPoint;
private float throwForce;
private float throwAngle;
private GameObject targetMark;
private Vector3 currentTargetPos;
private float launchAngle = 75f;
void Start()
{
if (!photonView.IsMine)
{
return;
}
targetMark = GameObject.Find("TargetMark");
}
void Update()
{
if (!photonView.IsMine)
{
return;
}
if (InputManager.isa.Player.Throw.triggered)
{
// ★改良(下記3つの攻撃はコメントアウトで止める)
// ThrowBomb();
// Targetshoot();
// SetDrone();
// ★追加
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;
public class AttackController : MonoBehaviourPunCallbacks
{
public GameObject shotPoint;
private float throwForce;
private float throwAngle;
private GameObject targetMark;
private Vector3 currentTargetPos;
private float launchAngle = 75f;
void Start()
{
if (!photonView.IsMine)
{
return;
}
targetMark = GameObject.Find("TargetMark");
}
void Update()
{
if (!photonView.IsMine)
{
return;
}
if (InputManager.isa.Player.Throw.triggered)
{
// ★改良(下記3つの攻撃はコメントアウトで止める)
// ThrowBomb();
// Targetshoot();
// SetDrone();
// ★追加
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);
}
}


アマテラスの設置