gpt4 book ai didi

c# - 敌人出现在玩家身后的机会 1 对 5

转载 作者:行者123 更新时间:2023-12-03 20:55:05 25 4
gpt4 key购买 nike

我写了一个脚本,让敌人出现在玩家身后,每 3 分钟就有五分之一的机会,出于测试目的,我将五分之一的机会更改为一分之一的机会,但我'我不确定它是否有效。

我知道如何将敌人置于玩家身后,但不知道如何以 1 对 5 的方式进行。

我用 1 到 5 之间的随机数进行了尝试。如果随机数等于 1,他就必须生成敌人,否则就不会。

这是代码:

using UnityEngine;
using System.Collections;
public class MainEnemy : MonoBehaviour
{
public GameObject Main;
public GameObject Holder;
public Transform player;
public Transform FPSController;
bool wantPosition;
bool appear;
float timer;
Vector3 temp;
// Use this for initialization

void Start()
{
wantPosition = true;
appear = false;
temp = new Vector3(0, 0, 0);
timer = 20;// 180;
}

// Update is called once per frame
void Update()
{
Appear();
}

Vector3 GetPosition()
{
Debug.Log("Hij doet het getposition");
float px = player.transform.position.x;
float pz = player.transform.position.z;
//
float angle2 = Mathf.Cos(Camera.main.transform.eulerAngles.y) + 180;
//
float distance = 20;
float distancex = Mathf.Cos(angle2 + 180) * distance;
float distancez = Mathf.Sin(angle2 + 180) * distance;
// Tell distanceen bij coordinaten op
temp = new Vector3(distancex, -3, distancez);
return temp;
}

void SetFalse()
{
if (timer < 1)
{
timer = 10;// 180; // na 3 minuten weer kijken of hij hem laat zien
}
Main.SetActive(false);
}

void Position()
{
if (wantPosition)
{
temp = GetPosition();
Holder.transform.position = player.position + temp;
Main.SetActive(true);
if (timer < 0)
{
timer = 10; // kort zichtbaar
}
wantPosition = false;
}
}

bool Appeared()
{
bool appear = false;
int rand = Random.Range(1, 1);
if (rand == 1)
{
appear = true;
}
else
{
appear = false;
}
return appear;
}

void Appear()
{
bool appear = false;
if (timer <= 0)
{
appear = Appeared();
}
else
{
timer -= Time.deltaTime;
if (timer < 10)
{
Debug.Log(timer);
}
}
if (appear == true)
{
Position();
}
else
{
SetFalse();
}
}
}

最佳答案

首先要注意Random.Range返回大于或等于 min 的数字,但小于(但不等于)max 。如果特殊情况其中min == max ,那么该值将被返回。

其次是你的Appeared函数有点冗长。假设我们将其设置回五分之一的机会,您将得到以下结果:

bool Appeared()
{
bool appear = false;
int rand = Random.Range(1, 6);
if (rand == 1)
{
appear = true;
}
else
{
appear = false;
}
return appear;
}

首先我要指出这句话:

if(x)
y = true
else
y = false

其中 x 是 bool 条件,与所说的完全相同

y = x

所以也许你可以将其更改为:

bool Appeared()
{
bool appear = false;
int rand = Random.Range(1, 6);
appear = rand == 1;
return appear;
}

现在看看,为什么要设置 appear一开始就为假?该值永远不会被使用。也许是这样:

bool Appeared()
{
int rand = Random.Range(1, 6);
bool appear = rand == 1;
return appear;
}

嗯,现在我们只是分配一个变量,然后在下一行返回它。好吧,也许是这样:

bool Appeared()
{
int rand = Random.Range(1, 6);
return rand == 1;
}

对,是的。这看起来很熟悉。现在,我们几乎只是分配一个变量,然后在下一行返回它。我们真的需要那个rand现在变量?可能不会。那么这个怎么样:

bool Appeared()
{
return Random.Range(1, 6) == 1;
}

好多了。

关于c# - 敌人出现在玩家身后的机会 1 对 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34175931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com