2017-12-13 9 views
-2

私は何をしようとしているのは、ランダムフラグが真であれば、ランダムにウェイポイントの間でプレイヤーを移動させることです。 しかし、ランダムメソッドを呼び出すだけでは不十分です。私はランダムが真であるならば、()を呼び出すRandomWayPointsAIチェックしています更新インサイドどのようにしてプレイヤーをウェイポイント間でランダムに移動できますか?

using System.Linq; 
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using System; 

public class Waypoints : MonoBehaviour 
{ 
    public GameObject[] waypoints; 
    public Transform target; 
    public float moveSpeed = 10f; 
    public float slowDownSpeed = 3f; 
    public float reverseSlowDownSpeed = 3f; 
    public float rotationSpeed = 1f; 
    private int targetsIndex = 0; 
    private Vector3 originalPosition; 
    private GameObject[] players; 

    public Transform reverseTarget; 
    private int reverseTargetsIndex = 0; 
    private Vector3 reverseOriginalPosition; 

    public bool random = false; 

    // Use this for initialization 
    void Start() 
    { 
     waypoints = GameObject.FindGameObjectsWithTag("Blocks"); 
     players = GameObject.FindGameObjectsWithTag("Player"); 
     originalPosition = players[0].transform.localPosition; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (random == true) 
     { 
      RandomWayPointsAI(); 
     } 
     else 
     { 
      WayPointsAI(); 
     } 
    } 

    private void WayPointsAI() 
    { 
     if (targetsIndex == waypoints.Length) 
      targetsIndex = 0; 
     target = waypoints[targetsIndex].transform; 
     float distance = Vector3.Distance(players[0].transform.position, target.transform.position); 
     players[0].transform.localRotation = Quaternion.Slerp(players[0].transform.localRotation, Quaternion.LookRotation(target.position - players[0].transform.localPosition), rotationSpeed * Time.deltaTime); 

     //move towards the player 
     if (distance < 30) 
     { 
      players[0].transform.localPosition += players[0].transform.forward * slowDownSpeed * Time.deltaTime; 
     } 
     else 
     { 
      players[0].transform.localPosition += players[0].transform.forward * moveSpeed * Time.deltaTime; 
     } 
     if (distance < target.transform.localScale.magnitude) 
     { 
      targetsIndex++; 
     } 
    } 

    private void ReverseWayPointsAI() 
    { 
     if (reverseTargetsIndex == 0) 
      reverseTargetsIndex = waypoints.Length - 1; 
     reverseTarget = waypoints[reverseTargetsIndex].transform; 
     float distance = Vector3.Distance(players[1].transform.position, reverseTarget.transform.position); 
     players[1].transform.rotation = Quaternion.Slerp(players[1].transform.rotation, Quaternion.LookRotation(reverseTarget.position - players[1].transform.position), rotationSpeed * Time.deltaTime); 

     //move towards the player 
     if (distance < 30) 
     { 
      players[1].transform.position += players[1].transform.forward * reverseSlowDownSpeed * Time.deltaTime; 
     } 
     else 
     { 
      players[1].transform.position += players[1].transform.forward * moveSpeed * Time.deltaTime; 
     } 
     if (distance < reverseTarget.transform.localScale.magnitude) 
     { 
      reverseTargetsIndex--; 
     } 
    } 

    void RandomWayPointsAI() 
    { 
     if (random == true) 
     { 
      int index = UnityEngine.Random.Range(0, waypoints.Length); 
      target = waypoints[index].transform; 
     } 
    } 

    void DrawLinesInScene() 
    { 
     // draw lines between each checkpoint // 
     for (int i = 0; i < waypoints.Length - 1; i++) 
     { 
      Debug.DrawLine(waypoints[i].transform.position, waypoints[i + 1].transform.position, Color.blue); 
     } 

     // draw a line between the original transform start position 
     // and the current transform position // 
     Debug.DrawLine(originalPosition, players[0].transform.position, Color.red); 
     Debug.DrawLine(reverseOriginalPosition, players[1].transform.position, Color.red); 

     // draw a line between current transform position and the next waypoint target 
     // each time reached a waypoint. 
     if (target != null) 
      Debug.DrawLine(target.transform.position, players[0].transform.position, Color.green); 
     if (reverseTarget != null) 
      Debug.DrawLine(reverseTarget.transform.position, players[1].transform.position, Color.green); 
    } 

    void AddColliderToWaypoints() 
    { 
     foreach (GameObject go in waypoints) 
     { 
      SphereCollider sc = go.AddComponent<SphereCollider>() as SphereCollider; 
      sc.isTrigger = true; 
     } 
    } 
} 

。それは単に各フレームを新しいランダムなウェイポイントを拾い続けているが、それはそれをプレーヤを動かすのではない。

void Update() 
    { 
     if (random == true) 
     { 
      RandomWayPointsAI(); 
     } 
     else 
     { 
      WayPointsAI(); 
     } 
    } 

答えて

1

ここにあなたの答えがあります。まずランダム関数に動きのコードを書いておらず、移動することを期待しています。

bool getNextRandom = true; 
void RandomWayPointsAI() 
{ 
    if (random == true && getNextRandom) 
    { 
     int index = UnityEngine.Random.Range(0, waypoints.Length); 
     target = waypoints[index].transform; 
     getNextRandom = false; 
    } 
    float distance = Vector3.Distance(players[0].transform.position, target.transform.position); 
    players[0].transform.localRotation = Quaternion.Slerp(players[0].transform.localRotation, Quaternion.LookRotation(target.position - players[0].transform.localPosition), rotationSpeed * Time.deltaTime); 

    //move towards the player 
    if (distance < 30) 
    { 
     players[0].transform.localPosition += players[0].transform.forward * slowDownSpeed * Time.deltaTime; 
    } 
    else 
    { 
     players[0].transform.localPosition += players[0].transform.forward * moveSpeed * Time.deltaTime; 
    } 
    if (distance < target.transform.localScale.magnitude) 
    { 
     getNextRandom = true; 
    } 
} 

はさらに、あなたはあなたのよりよい制御を与える別の関数への移動コードを移動することにより溶液をimporveでき

public GameObject[] waypoints; 
public Transform target; 
public float moveSpeed = 10f; 
public float slowDownSpeed = 3f; 
public float reverseSlowDownSpeed = 3f; 
public float rotationSpeed = 1f; 
private int targetsIndex = 0; 
private Vector3 originalPosition; 
private GameObject[] players; 
public Transform reverseTarget; 
private int reverseTargetsIndex = 0; 
private Vector3 reverseOriginalPosition; 
public bool random = false; 
public bool getNextRandom = true; 

// Use this for initialization 
void Start() 
{ 
    waypoints = GameObject.FindGameObjectsWithTag("Blocks"); 
    players = GameObject.FindGameObjectsWithTag("Player"); 
    originalPosition = players[0].transform.localPosition; 
} 

// Update is called once per frame 
void Update() 
{ 
    if (random == true) 
    { 
     RandomWayPointsAI(); 
    } 
    else 
    { 
     WayPointsAI(); 
    } 
} 

private void WayPointsAI() 
{ 
    if (targetsIndex == waypoints.Length) 
     targetsIndex = 0; 
    target = waypoints[targetsIndex].transform; 

    if (MovePlayer()) 
     targetsIndex++; 
} 

private void ReverseWayPointsAI() 
{ 
    if (reverseTargetsIndex == 0) 
     reverseTargetsIndex = waypoints.Length - 1; 
    reverseTarget = waypoints[reverseTargetsIndex].transform; 

    if (MovePlayer()) 
     reverseTargetsIndex--; 
} 


void RandomWayPointsAI() 
{ 
    if (random == true && getNextRandom) 
    { 
     int index = UnityEngine.Random.Range(0, waypoints.Length); 
     target = waypoints[index].transform; 
     getNextRandom = false; 
    } 
    getNextRandom = MovePlayer(); 
} 

bool MovePlayer() 
{ 
    float distance = Vector3.Distance(players[0].transform.position, target.transform.position); 
    players[0].transform.localRotation = Quaternion.Slerp(players[0].transform.localRotation, Quaternion.LookRotation(target.position - players[0].transform.localPosition), rotationSpeed * Time.deltaTime); 

    //move towards the player 
    if (distance < 30) 
    { 
     players[0].transform.localPosition += players[0].transform.forward * slowDownSpeed * Time.deltaTime; 
    } 
    else 
    { 
     players[0].transform.localPosition += players[0].transform.forward * moveSpeed * Time.deltaTime; 
    } 
    if (distance < target.transform.localScale.magnitude) 
     return true; 
    else 
     return false; 

} 

void DrawLinesInScene() 
{ 
    // draw lines between each checkpoint // 
    for (int i = 0; i < waypoints.Length - 1; i++) 
    { 
     Debug.DrawLine(waypoints[i].transform.position, waypoints[i + 1].transform.position, Color.blue); 
    } 

    // draw a line between the original transform start position 
    // and the current transform position // 
    Debug.DrawLine(originalPosition, players[0].transform.position, Color.red); 
    Debug.DrawLine(reverseOriginalPosition, players[1].transform.position, Color.red); 

    // draw a line between current transform position and the next waypoint target 
    // each time reached a waypoint. 
    if (target != null) 
     Debug.DrawLine(target.transform.position, players[0].transform.position, Color.green); 
    if (reverseTarget != null) 
     Debug.DrawLine(reverseTarget.transform.position, players[1].transform.position, Color.green); 
} 

void AddColliderToWaypoints() 
{ 
    foreach (GameObject go in waypoints) 
    { 
     SphereCollider sc = go.AddComponent<SphereCollider>() as SphereCollider; 
     sc.isTrigger = true; 
    } 
} 
1

RandomWayPointsAI()関数を見ると、インデックスとターゲットのみが定義されていますが、その下にはプレーヤーを移動するためのコードはありません。

private void WayPointsAI() 
{ 
    if (targetsIndex == waypoints.Length) 
     targetsIndex = 0; 
    target = waypoints[targetsIndex].transform; 
    float distance = Vector3.Distance(players[0].transform.position, target.transform.position); 
    players[0].transform.localRotation = Quaternion.Slerp(players[0].transform.localRotation, Quaternion.LookRotation(target.position - players[0].transform.localPosition), rotationSpeed * Time.deltaTime); 


void RandomWayPointsAI() 
{ 
    //No need to check if random is true anymore, you already checked when you run this function 
    int index = UnityEngine.Random.Range(0, waypoints.Length); 
    target = waypoints[index].transform; 
    //float distance = Vector3.Distance(players[0].transform.position, target.transform.position); 
    //players[0].transform.localRotation = Quaternion.Slerp(players[0].transform.localRotation, Quaternion.LookRotation(target.position - players[0].transform.localPosition), rotationSpeed * Time.deltaTime);** 
} 
関連する問題