2017-06-30 63 views
0

所以我正在從Spectrum製作遊戲「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; 
     } 
    } 
} 
+1

通常在這些遊戲的邏輯是徘徊無論在AI是,取1步步接近玩家角色..無論它是.. – BugFinder

+0

@BugFinder好有多個蛇,這會讓遊戲太難,我不介意測試和調整。你能幫助我嗎?但是,除了這個功能之外,如果蛇在旁邊,蛇會殺死玩家。現在他們只是要去的地方,並不真正關心球員。 – Shanespeed

+0

我不知道那是什麼蛇遊戲,但爲什麼不使用對撞機? –

回答

0

在蛇頭下創建空的gameobject,並將其稱爲'DetectRange'或類似的東西。然後在巡視器中設置它的寬度和高度,以使您的蛇跟隨玩家的範圍的寬度和高度。所以如果玩家進入該範圍,蛇就會開始跟隨。然後你可以將玩家對象的標籤設置爲「玩家」或類似的東西(這是在檢查員的頂部)。然後您將腳本到DetectRange和裏面你做:

public GameObject playerObject; //Drag player gameobject in inspector 
private float snakeSpeed = 1.0f; 


void OnTriggerStay(Collider col) 
{ 
    if(col.gameObject.Tag == "Player") //Check if player entered collider 
    { 
     playerObject.transform.position = Vector3.MoveTowards(playerObject.transform.position, col.gameObject.position, snakeSpeed); 
    } 
} 
+0

謝謝!這一定會解決我的問題! – Shanespeed