当玩家发生冲突时添加炸弹数量

时间:2018-11-02 08:24:54

标签: c# unity3d

在发布此消息之前,我尝试使用搜索栏,但没有发现任何实际可以解决问题的方法。

当玩家经过“物品”时,它会按预期消失,但是我也希望它也可以在物品被拾取时向当前编号为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()
{

}
}

2 个答案:

答案 0 :(得分:2)

首先,将脚本更改为以下内容。 如果您仍未在控制台中看到no_cell的增加,请给我们一些屏幕截图,从编辑器显示与游戏对象相连的脚本以及您希望看到差异的地方。

public class shooter : MonoBehaviour {
    public GameObject powercell;    //link to the powerCell prefab
    public int no_cell ;            //number of powerCell owned
    public AudioClip throwSound;    //throw sound
    public float throwSpeed = 20;   //throw speed

    // Use this for initialization
    void Start(){
        no_cell = 1;
    }

    // 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++;              
            Debug.Log("Number of cells:"+no_cell.ToString());
            Destroy(other);
        }
    }


}

答案 1 :(得分:0)

如果我说得对,每次您与“项目”发生冲突时,您想要添加1个额外的单元格吗? 然后,您应该使用no_cell ++(或者,如果您的最大no_cell应该为1,则保持原样)。

还要检查其他条件是否会减少您的no_cell,所以这可能是您看不到这种情况有所增加的原因。

最后,建议的技巧是使用Start()进行初始化(例如no_cell = 1),如果no_cell引用了单元格的数量,则使用更接近自然语言的方便命名方式,例如cellsNumber。