Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何编写代码来扣减一个生命点?_C#_Unity3d - Fatal编程技术网

C# 如何编写代码来扣减一个生命点?

C# 如何编写代码来扣减一个生命点?,c#,unity3d,C#,Unity3d,在单位里训练一名篮球射手。我在做负责减去生命细胞的部分。当您触摸“奖金”组件并添加积分时,将触发负责添加积分的代码部分,但负责降低生命值的代码下一部分不起作用。根据设计,这应该发生在球没有击中“奖励”对象并与“地形”对象接触之后。如何编写一个代码,如果没有接触到“奖金”对象,然后接触到“地形”对象,则减去一个生命点 public GameObject hp3; public GameObject hp2; public GameObject hp1; int score = 0; int be

在单位里训练一名篮球射手。我在做负责减去生命细胞的部分。当您触摸“奖金”组件并添加积分时,将触发负责添加积分的代码部分,但负责降低生命值的代码下一部分不起作用。根据设计,这应该发生在球没有击中“奖励”对象并与“地形”对象接触之后。如何编写一个代码,如果没有接触到“奖金”对象,然后接触到“地形”对象,则减去一个生命点

public GameObject hp3;
public GameObject hp2;
public GameObject hp1;

int score = 0;
int best = 0;

void Awake()
{
    rb = GetComponent<Rigidbody>();
    col = GetComponent<Collider>();

    //instance = this;
}

private void Start()
{
    best = PlayerPrefs.GetInt("best", 0);

    scoreText.text = "Score: " + score.ToString();
    bestScoreText.text = "Best: " + best.ToString();
}

public void Push(Vector2 force)
{
    rb.AddForce(force, ForceMode.Impulse);
}

public void ActivateRb()
{
    rb.isKinematic = false;
}

public void DesactivateRb()
{
    rb.velocity = Vector3.zero;
    //rb.angularVelocity = 0f;
    rb.isKinematic = true;
}

private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.name == "Terraine")
    {
        transform.position = new Vector3(Random.Range(-4f, 8f), Random.Range(2f, 7f), -0.2f);
        DesactivateRb();
    }
}

public void OnTriggerExit(Collider other)
{
    if (other.gameObject.name == "Bonus")
    {
        score += 2;
        scoreText.text = "Score: " + score.ToString();
        if (best < score)
            PlayerPrefs.SetInt("best", score);
    }

    if (other.gameObject.name != "Bonus")
    {
        health = health - 1;
    }
}

void Update()
{
    if (health == 3)
    {
        hp3.SetActive(true);
        hp2.SetActive(false);
        hp1.SetActive(false);
    }
    if (health == 2)
    {
        hp3.SetActive(false);
        hp2.SetActive(true);
        hp1.SetActive(false);
    }
    if (health == 1)
    {
        hp3.SetActive(false);
        hp2.SetActive(false);
        hp1.SetActive(true);
    }
}
公共游戏对象hp3;
公共游戏对象hp2;
公共游戏对象hp1;
智力得分=0;
int-best=0;
无效唤醒()
{
rb=GetComponent();
col=GetComponent();
//实例=此;
}
私有void Start()
{
最佳=PlayerPrefs.GetInt(“最佳”,0);
scoreText.text=“Score:+Score.ToString();
bestScoreText.text=“最佳:”+Best.ToString();
}
公共无效推送(矢量2力)
{
rb.附加力(力、力模式、脉冲);
}
公共无效激活器b()
{
rb.iskinetic=false;
}
公共无效的DeActivaterB()
{
rb.速度=矢量3.0;
//rb.角速度=0f;
rb.iskinetic=true;
}
专用void OnCollisionEnter(碰撞)
{
如果(collision.gameObject.name==“Terraine”)
{
transform.position=新矢量3(随机范围(-4f,8f),随机范围(2f,7f),-0.2f);
去活化剂b();
}
}
公共无效OnTriggerExit(对撞机其他)
{
如果(other.gameObject.name==“奖金”)
{
分数+=2分;
scoreText.text=“Score:+Score.ToString();
如果(最佳<分数)
PlayerPrefs.SetInt(“最佳”,分数);
}
如果(other.gameObject.name!=“奖金”)
{
健康=健康-1;
}
}
无效更新()
{
如果(健康==3)
{
hp3.SetActive(真);
hp2.SetActive(假);
hp1.SetActive(假);
}
如果(运行状况==2)
{
hp3.SetActive(假);
hp2.SetActive(真);
hp1.SetActive(假);
}
如果(运行状况==1)
{
hp3.SetActive(假);
hp2.SetActive(假);
hp1.SetActive(真);
}
}
我不知道c,但像这样简单的东西行得通吗? 没有逻辑很难知道

private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.name == "Terraine")
    {
        transform.position = new Vector3(Random.Range(-4f, 8f), Random.Range(2f, 7f), -0.2f);
        DesactivateRb();
        if (flag != 1)
        {
        health = health - 1;
        }
        else
        {
        flag = 0;    //reset flag if 1
        }
    }
}

public void OnTriggerExit(Collider other)
{
    if (other.gameObject.name == "Bonus")
    {
        score += 2;
        flag = 1;
        scoreText.text = "Score: " + score.ToString();
        if (best < score)
            PlayerPrefs.SetInt("best", score);
    }

}
private void OnCollisionEnter(冲突)
{
如果(collision.gameObject.name==“Terraine”)
{
transform.position=新矢量3(随机范围(-4f,8f),随机范围(2f,7f),-0.2f);
去活化剂b();
如果(标志!=1)
{
健康=健康-1;
}
其他的
{
flag=0;//如果为1,则重置标志
}
}
}
公共无效OnTriggerExit(对撞机其他)
{
如果(other.gameObject.name==“奖金”)
{
分数+=2分;
flag=1;
scoreText.text=“Score:+Score.ToString();
如果(最佳<分数)
PlayerPrefs.SetInt(“最佳”,分数);
}
}

我会与各州合作。首先,您处于状态,例如,
等待奖金
->如果您击中奖金HaveHitBonus->与
地形
->碰撞没有问题。如果你没有命中奖励,那么状态仍然是
WaitingForBonus
->与地形碰撞->哎哟!击中地形后,始终返回
等待奖励