- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我写了一个脚本,让敌人出现在玩家身后,每 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/
好的,这就是我的错误:'Enemy' 未在此范围内声明。错误在 map.h 文件中,即使 map.h 包含 enemy.h,如图所示 #ifndef MAP_H_INCLUDED #define MA
这个问题已经有答案了: How to make enemy follow player in pygame? (2 个回答) Enemy doesn't follow player (pygame)
我试图让我的 Canvas 游戏中的 Sprite 不断向玩家移动,直到它发生碰撞。执行此操作的相关函数是 update() 函数: Enemy.prototype.update = function
我在无休止的运行游戏中遇到了麻烦,主要是我创建的两种敌人的产卵,当我们调用函数“startDifficultyTimer”时,有时会产生两个敌人。 基本上,当我调用该函数时,游戏会在同一行生成 2 个
从性能角度来看,什么是最正确的 - 敌人和武器之间的交互算法(这里的子弹可能更正确)? 每个 Sprite 的每一个子弹都应该通过“collidesWith”方法检查碰撞情况,并在完整的敌人列表上进行
遇到了一个问题,即敌人会向玩家开火,但似乎总是开到高处或向玩家的一侧,即使玩家是静止的并且没有移动。我是不是在我的代码中做错了什么导致了这个疯狂的问题,或者它只是一个随机的烦人的错误? 为播放器使用相
我是新来的快速编程。我想使用类节点将敌人生成到随机位置。我试图搜索随机生成敌人的代码,但它似乎与我的代码无关。 这是我搜索随机生成的代码。 import SpriteKit class GameSce
我最近遇到了一些问题,只是在我正在制作的游戏中传递对对象/敌人的引用,我想知道我是否使用了错误的方法。 我的主要问题是处理敌人和物体,而其他敌人或玩家可能仍然与它们有联系。 例如,如果你有一只兔子和一
在我的 2d 游戏中,我有一个金币和铜币的概念如下: when normal enemies die ..they drop a random gold or copper coin. Now whe
我是新来的,所以我希望你能帮助我。 我正在学习制作简单的 cocos2d 游戏的教程 Ray Wenderlich's Tutorial 我在另一个游戏中实现了它,一个像涂鸦跳跃这样的跳跃游戏。 在上
我是一名优秀的程序员,十分优秀!