私はスペクトラムからゲーム "Snake Pit"のレプリカを作っています。あなたはあなたのプレイヤーとして移動でき、複数のAIコントロールヘビがいます。そして、あなたのすぐ隣にいるときにスネークヘッドを自分の位置に移動させる方法を見つけようとしています。ここに私が実装したいコードがあります。AIの範囲内の特定のオブジェクトを検出するにはどうすればよいですか?
public class SnakeController : MonoBehaviour {
public int maxSize;
public int currentSize;
public GameObject snakePrefab;
public Snake Head;
public Snake Tail;
public Vector2 nextPos;
public int NESW;
int Random;
float lineTimer;
int NESWTemp;
// Use this for initialization
void Start() {
InvokeRepeating("TimerInvoke", 0, .3f);
lineTimer = UnityEngine.Random.Range(0, 2.5f);
currentSize = 1;
}
// Update is called once per frame
void Update() {
lineTimer -= Time.deltaTime;
if (lineTimer <= 0)
{
ComChangeD();
lineTimer = UnityEngine.Random.Range(0, 2.5f);
}
}
void TimerInvoke()
{
Movement();
if(currentSize >= maxSize)
{
TailFunction();
}
else
{
currentSize++;
}
}
void Movement()
{
GameObject temp;
nextPos = Head.transform.position;
if(nextPos.y > 3.22)
{
NESW = 2;
}
else if (nextPos.y < -4.42)
{
NESW = 0;
}
else if (nextPos.x < -8.2)
{
NESW = 1;
}
else if (nextPos.x > 7.7)
{
NESW = 3;
}
switch (NESW)
{
case 0:
nextPos = new Vector2(nextPos.x, nextPos.y + 0.32f);
break;
case 1:
nextPos = new Vector2(nextPos.x + 0.32f, nextPos.y);
break;
case 2:
nextPos = new Vector2(nextPos.x, nextPos.y - 0.32f);
break;
case 3:
nextPos = new Vector2(nextPos.x - 0.32f, nextPos.y);
break;
}
temp = (GameObject)Instantiate(snakePrefab, nextPos, transform.rotation);
Head.SetNext(temp.GetComponent<Snake>());
Head = temp.GetComponent<Snake>();
return;
}
void ComChangeD()
{
NESWTemp = UnityEngine.Random.Range(0, 3);
if (NESW == 0)
{
switch (NESWTemp)
{
case 0:
NESW = 1;
break;
case 1:
NESW = 2;
break;
case 2:
NESW = 3;
break;
}
}
else if (NESW == 1)
{
switch (NESWTemp)
{
case 0:
NESW = 0;
break;
case 1:
NESW = 2;
break;
case 2:
NESW = 3;
break;
}
}
else if (NESW == 2)
{
switch (NESWTemp)
{
case 0:
NESW = 0;
break;
case 1:
NESW = 1;
break;
case 2:
NESW = 3;
break;
}
}
else if (NESW == 3)
{
switch (NESWTemp)
{
case 0:
NESW = 0;
break;
case 1:
NESW = 1;
break;
case 2:
NESW = 2;
break;
}
}
}
通常、これらのゲーム内のロジックがあり、それは... – BugFinder
@BugFinderまあでどこ..近いプレイヤーキャラクタへの1歩を踏み出します複数のヘビがあり、それがゲームを難しくするだろう、私はそれをテストし、調整することはありません。おそらくそれで私を助けることができますか?しかし、この機能を除いて、プレイヤーが隣にいればヘビがプレイヤーを殺すようになっています。今、彼らはただの場所に行き、プレイヤーを気にしません。 – Shanespeed
私はその蛇のゲームが何であるかわからないが、なぜコライダーを使用しないのですか? –