我写了一个脚本,让敌人出现在玩家身后,每 3 分钟有 1 次机会,我已经将 1 次 5 机会更改为 1 次机会以进行测试,但我不确定是否有用。
我知道如何将敌人置于玩家身后,但没有 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();
}
}
}