Bombを投げる











ボムを投げる
using UnityEngine;
// ★追加
using Photon.Pun;
public class AttackController : MonoBehaviourPunCallbacks // ★変更
{
public GameObject shotPoint;
private float throwForce;
private float throwAngle;
void Update()
{
if (!photonView.IsMine)
{
return;
}
if (InputManager.isa.Player.Throw.triggered)
{
ThrowBomb();
}
}
void ThrowBomb()
{
throwForce = 10f;
throwAngle = 45f;
GameObject bomb = PhotonNetwork.Instantiate("Bomb", shotPoint.transform.position, Quaternion.identity);
Rigidbody bombRb = bomb.GetComponent<Rigidbody>();
// 角度を計算(Quaternion.AngleAxisを使用して向きを上に傾ける)
// 自身の右軸(transform.right)を中心に、設定した角度分だけ回転させる
Vector3 forceDirection = Quaternion.AngleAxis(-throwAngle, transform.right) * transform.forward;
// 力を加える(質量を無視して速度を直接変えるVelocityChangeを利用)
bombRb.AddForce(forceDirection * throwForce, ForceMode.VelocityChange);
}
}


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










ボムを投げる
using UnityEngine;
// ★追加
using Photon.Pun;
public class AttackController : MonoBehaviourPunCallbacks // ★変更
{
public GameObject shotPoint;
private float throwForce;
private float throwAngle;
void Update()
{
if (!photonView.IsMine)
{
return;
}
if (InputManager.isa.Player.Throw.triggered)
{
ThrowBomb();
}
}
void ThrowBomb()
{
throwForce = 10f;
throwAngle = 45f;
GameObject bomb = PhotonNetwork.Instantiate("Bomb", shotPoint.transform.position, Quaternion.identity);
Rigidbody bombRb = bomb.GetComponent<Rigidbody>();
// 角度を計算(Quaternion.AngleAxisを使用して向きを上に傾ける)
// 自身の右軸(transform.right)を中心に、設定した角度分だけ回転させる
Vector3 forceDirection = Quaternion.AngleAxis(-throwAngle, transform.right) * transform.forward;
// 力を加える(質量を無視して速度を直接変えるVelocityChangeを利用)
bombRb.AddForce(forceDirection * throwForce, ForceMode.VelocityChange);
}
}


Bombを投げる