C# 摧毁精确目标

C# 摧毁精确目标,c#,unity3d,C#,Unity3d,我正在创建一个小2d游戏,你需要在其中生存。每棵树都有自己的强度=5。当玩家碰撞并按下鼠标左键时,力量值为-1,玩家木属性值为+1。当树的强度等于或小于0时,树将被破坏。这是我的密码:(问题在密码后面) 使用System.Collections.Generic; 使用UnityEngine; 使用UnityEngine.UI; 公共类统计:单一行为 { //球员统计 公共浮点数hp=100; 公共浮木=0; //树统计 公共浮标treeLogStrenth=5; //正文 公共文本; 无效开始(

我正在创建一个小2d游戏,你需要在其中生存。每棵树都有自己的强度=5。当玩家碰撞并按下鼠标左键时,力量值为-1,玩家木属性值为+1。当树的强度等于或小于0时,树将被破坏。这是我的密码:(问题在密码后面)

使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类统计:单一行为
{
//球员统计
公共浮点数hp=100;
公共浮木=0;
//树统计
公共浮标treeLogStrenth=5;
//正文
公共文本;
无效开始()
{
woodText.text=“0”;
}
无效更新()
{
woodText.text=wood.ToString();

如果(treeLogStrenth那么为了澄清问题:
Stats
应该附加到玩家身上,对吗

您不应该在
Update
中的每一帧都做一些事情,而应该像

public class Stats : MonoBehaviour
{
    // You should never allow your stats to be set via public fields
    [SerializeField] private float hp = 100;
    [SerializeField] private float wood = 0;

    // if you need to read them from somewhere else you can use read-only properties
    public float HP => hp;
    public float Wood => wood;

    [SerializeField] private Text woodText;

    private void Start ()
    {
        woodText.text = "0";
    }

    public void AddWood(int amount)
    {
        wood += amount;
        woodText.text = wood.ToString();
    }
}
然后每个树应该有自己的组件实例,比如

public class Tree : MonoBehaviour
{
    [SerializeField] private float treeLogStrenth = 5;

    public void HandleClick(Stats playerStats)
    {
        // if this tree has wood left add it to the player stats
        if(treeLogStrength > 0)
        {
            playerStats.AddWood(1); 
            treeLogStrenth -= 1;
        }

        // destroy this tree when no wood left
        if (treeLogStrenth <= 0)
        {
            Destroy(gameObject);
        }
    }
}
公共类树:单行为
{
[SerializeField]专用浮点数treeLogStrenth=5;
公共无效HandleClick(统计玩家统计)
{
//如果这棵树还剩下木头,则将其添加到玩家统计数据中
如果(树形强度>0)
{
阿德伍德(1);
treeLogStrenth-=1;
}
//在没有木头的时候把这棵树毁掉

如果(treeLogStrenth)这是JavaScript?-我删除了JavaScript标记,因为这是JAVA或已经标记的C.;
是不完整的代码你真的在使用Visual Studio 2010吗?!不管怎样,标签都是不需要的。我不明白你的问题。当你需要100棵树时,你必须创建100棵树才能摧毁它们。当然每棵树都有自己的碰撞器,所以你可以确定你碰撞了哪些树。@MaxCieplinski哪部分?等等这意味着每棵树都应该有自己的组件?如果有50个,那么代码就太多了trees@MaxCieplinski好吧,如果有50棵树,那么就有50个游戏对象带有
组件。是的,但是有没有办法不让每棵树都保持不变?@MaxCieplinski我不明白你的意思……如果每棵树都应该每个树都有自己的生命周期和可以接受的点击量,以便提供木材,那么没有…每个树都需要一个
组件的单独实例,以便处理与其相关的值。
public class Stats : MonoBehaviour
{
    // You should never allow your stats to be set via public fields
    [SerializeField] private float hp = 100;
    [SerializeField] private float wood = 0;

    // if you need to read them from somewhere else you can use read-only properties
    public float HP => hp;
    public float Wood => wood;

    [SerializeField] private Text woodText;

    private void Start ()
    {
        woodText.text = "0";
    }

    public void AddWood(int amount)
    {
        wood += amount;
        woodText.text = wood.ToString();
    }
}
public class Tree : MonoBehaviour
{
    [SerializeField] private float treeLogStrenth = 5;

    public void HandleClick(Stats playerStats)
    {
        // if this tree has wood left add it to the player stats
        if(treeLogStrength > 0)
        {
            playerStats.AddWood(1); 
            treeLogStrenth -= 1;
        }

        // destroy this tree when no wood left
        if (treeLogStrenth <= 0)
        {
            Destroy(gameObject);
        }
    }
}
public class PlayerCollisions: MonoBehaviour
{
    // better already reference this via the Inspector
    [SerializeField] private Stats stats;

    // will store the currently collided tree in order to reuse it
    private Tree currentlyCollidedTree;

    // as fallback initialize it on runtime
    private void Awake()
    {
        if(!stats) stats = GetComponent<Stats>();
    }

    private void OnCollisionStay2D(Collision2D collisionInfo)
    {
        if (collisionInfo.gameObject.CompareTag("Tree") && Input.GetMouseButtonDown(0))
        {
            // Get the Tree component of the tree object you are currently colliding with
            // but only once and store the reference in order to reuse it
            if(!currentlyCollidedTree) currentlyCollidedTree= collisionInfo.gameObject.GetComponent<Tree>();

            // tell the tree to handle a click and pass in your stats reference
            currentlyCollidedTree.HandleClick(stats);
        }
    }

    // reset the currentlyCollidedTree field when not colliding anymore
    private void OnCollisionExit2D()
    {
        currentlyCollidedTree = null;
    }
}
public class PlayerCollisions: MonoBehaviour
{
    // better already reference this via the Inspector
    [SerializeField] private Stats stats;

    // will store all trees we ever clicked on in relation to the according available wood
    private Dictionary<GameObject, int> encounteredTrees = new Dictionary<GameObject, int>();

    // as fallback initialize it on runtime
    private void Awake()
    {
        if(!stats) stats = GetComponent<Stats>();
    }

    private void OnCollisionStay2D(Collision2D collisionInfo)
    {
        if (collisionInfo.gameObject.CompareTag("Tree") && Input.GetMouseButtonDown(0))
        {
            // did we work on this tree before?
            if(encounteredTrees.Contains(collisionInfo.gameObject))
            {
                // if so gain one wood and remove one from this tree
                stats.AddWood(1);
                encounteredTrees[collisionInfo.gameObject] -= 1;
                // destroy the tree if no wood available and remove it from the dictionary
                if(encounteredTrees[collisionInfo.gameObject] <= 0)
                {
                    encounteredTrees.RemoveKey(collisionInfo.gameObject);
                    Destroy(collisionInfo.gameObject);
                }
            }
            else
            {
                // the first time we work this tree gain one wood and add
                // the tree as new entry to the dictionary with 4 wood left
                stats.AddWood(1);
                encounteredTrees.Add(collisionInfo.gameObject, 4);
            }
        }
    }
}