我希望每把弓都射出我在創建時在 SO 中設定的不同箭頭。我需要每個弓來射擊 SO 中指定的箭頭預制件。
WeaponConfig 有[SerializeField] public GameObject projectile;我希望能夠換出不同箭頭的欄位,因此每種新武器都可以射出我將在檢查器中設定的不同箭頭。
拍攝腳本:
public void ShootingArrows()
{
var screenCam = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 worldMousePos = screenCam;
// Direction of mouse to player
Vector2 direction = (Vector2)((worldMousePos - firePoint.position));
direction.Normalize();
// Creates the arrow locally
GameObject arrow = (GameObject)Instantiate (
arrowPrefab,
firePoint.position (Vector3)( direction * 0.5f),
firePoint.rotation);
FindObjectOfType<AudioManager>().Play("BowShoot");
// Adds velocity to the arrow
arrow.GetComponent<Rigidbody2D>().velocity = direction * arrowForce;
// Rotate arrow based on position from player to cursor
angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90f;
arrow.transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), 1000 * Time.deltaTime);
}
在我的實體化中,我arrowPrefab將其設定為腳本頂部的游戲物件。
我將其更改為什么,以實體化預期的射彈?
如果我將其更改為下面的代碼,則表示它沒有物件參考。
GameObject arrow = (GameObject)Instantiate (
WeaponConfig.projectile,
firePoint.position (Vector3)( direction * 0.5f),
firePoint.rotation);
我在檢查器中的 Projectile 插槽中填充了我想要射擊的預期箭頭。每個都選擇了不同的預制件。如何在 SO 中實體化分配的箭頭?

解決方案:我的 ShootingArrows 函式沒有對 WeaponConfig 的參考...
public void ShootingArrows(WeaponConfig weapon){
...
Projectile arrow = Instantiate (
weapon.projectile,
firePoint.position (Vector3)(
direction * 0.5f),
firePoint.rotation);
}
uj5u.com熱心網友回復:
好吧,而不是到目前為止
public GameObject arrowPrefab;
你需要有一個
public WeaponConfig config:
欄位,在那里參考您的Bow資產,然后執行
GameObject arrow = Instantiate (
config.projectile,
firePoint.position (Vector3)(direction * 0.5f),
firePoint.rotation);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/424491.html
