【发布时间】:2018-08-09 17:43:51
【问题描述】:
我在 2d 横向卷轴游戏中遇到了一个奇怪的问题,Unity 有时只会在按下火力时创建我的弹丸克隆,它会正确地从库存中移除 1 颗手榴弹,无论手榴弹是否被克隆或不是。 这是我的代码
void ThrowGranade()
{
if (grenandeInventory > 0)
{
GameObject grenade = Instantiate(projectile, projectileSpawnPoint.transform.position, Quaternion.identity) as GameObject;
grenandeInventory =- 1;
//am.ThrowGrenade();
}
else if (grenandeInventory <= 0)
{
grenandeInventory = 0;
}
}
以及更新函数中保存的开火按钮脚本
if (Input.GetButtonDown("Fire1"))
{
if (grenandeInventory>0)
{
grenandeInventory -= 1;
ThrowGranade();
}
}
我在弹丸本身的启动功能中添加了力
void Start () {
#region REFERENCES
anim = GetComponent<Animator>();
am = FindObjectOfType<AudioManager>();
rb = GetComponent<Rigidbody2D>();
player = FindObjectOfType<PlayerScript>();
capsule = GetComponent<CapsuleCollider2D>();
#endregion
rb.AddForce(Vector2.right * player.projectileForce, ForceMode2D.Force);
}
【问题讨论】:
-
你为什么要减少你的 grenandeInventory 变量两次?如果它的值为 1,它将调用 ThrowGranade() 方法,但不会实例化新的手榴弹。是故意的吗?
-
不,这并不是我在沮丧中不断经过的第二个问题。很好的收获。
标签: unity3d clone instantiation projectile