【发布时间】:2019-04-06 11:41:27
【问题描述】:
我在发布此内容之前尝试使用搜索栏,但没有发现任何东西能真正解决我的问题。
当玩家走过“物品”时,它会按预期消失,但是我也希望它在拾取该物品时也向当前编号为 0 的玩家添加一颗炸弹,但它似乎不是在职的?非常感谢任何帮助。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class shooter : MonoBehaviour
{
public GameObject powercell; //link to the powerCell prefab
public int no_cell = 1; //number of powerCell owned
public AudioClip throwSound; //throw sound
public float throwSpeed = 20;//throw speed
// Update is called once per frame
public void Update()
{
//if left control (fire1) pressed, and we still have at least 1 cell
if (Input.GetButtonDown("Fire1") && no_cell > 0)
{
no_cell--; //reduce the cell
//play throw sound
AudioSource.PlayClipAtPoint(throwSound, transform.position);
//instantaite the power cel as game object
GameObject cell = Instantiate(powercell, transform.position,
transform.rotation) as GameObject;
//ask physics engine to ignore collison between
//power cell and our FPSControler
Physics.IgnoreCollision(transform.root.GetComponent<Collider>(),
cell.GetComponent<Collider>(), true);
//give the powerCell a velocity so that it moves forward
cell.GetComponent<Rigidbody>().velocity = transform.forward * throwSpeed;
}
}
//
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Item")
{
no_cell = 1;
Destroy(gameObject);
}
}
// Use this for initialization
void Start()
{
}
}
【问题讨论】:
-
你在哪一行代码中增加了炸弹的数量?
-
@MiladQasemi 我认为'no_cell = 1;'碰撞时会将 int 增加 1 吗?
-
好吧 no_cell = 1 将该值设置为 1 但它不显示炸弹它只是更改变量的值
-
当然不是,你意识到
no_cell值只属于这个实例,所以在改变它之后你正在破坏shooter所以这个值被它破坏了所以你会看不到它,当您制作预制件的新实例时,它与被删除的实例无关。