【发布时间】:2020-08-02 21:21:26
【问题描述】:
我正在尝试为必须跟随玩家并定位在尚未被其他同伴占据的最近位置的同伴制作 AI
我在同伴中有一个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReachPosition : MonoBehaviour
{
public GameObject[] firstLine = new GameObject[2];
Transform target;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//calculate distance from each position
float dist = Vector3.Distance(transform.position, firstLine[0].transform.position);
float dist1 = Vector3.Distance(transform.position, firstLine[1].transform.position);
if (dist < dist1 && firstLine[0].GetComponent<Position>().isEmpty)
{
target = firstLine[0].transform;
Debug.Log(0);
}
else if (dist > dist1 && firstLine[1].GetComponent<Position>().isEmpty)
{
target = firstLine[1].transform;
Debug.Log(1);
}
//reach target
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), 1 * Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position, target.position, .01f);
}
}
和一个触发器的位置:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Position : MonoBehaviour
{
public bool isEmpty;
// Start is called before the first frame update
void Start()
{
isEmpty = true;
}
private void OnTriggerEnter(Collider other)
{
isEmpty = false;
}
private void OnTriggerExit(Collider other)
{
isEmpty = true;
}
}
实际上,他们到达最近的位置,但他们不在乎该位置是否被占用。 (我还应该尝试让代码为现在不存在的“firstLine”gameObject 数组正常工作)抱歉英语不好...
------编辑-------------- 我改变了选择到达位置的方式,正在编写编队脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Formation : MonoBehaviour
{
public List<GameObject> firstLine = new List<GameObject>();
public List<GameObject> Companions = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < Companions.Count; i++)
{
//set the position to reach to the first non occupied position
int a = 0;
while (!firstLine[a].GetComponent<Position>().isEmpty)
{
a++;
}
Companions[i].GetComponent<Companion>().objectToReach = firstLine[a];
Companions[i].GetComponent<Companion>().distanceFromTarget = Vector3.Distance(Companions[i].transform.position, firstLine[a].transform.position);
firstLine[a].GetComponent<Position>().SetEmptyOrNot(false);
//set the position to reach to the closest non occupied position
for (int j = 0; j < firstLine.Count; j++)
{
float dist = Vector3.Distance(Companions[i].transform.position, firstLine[j].transform.position);
if (dist < Companions[i].GetComponent<Companion>().distanceFromTarget && firstLine[j].GetComponent<Position>().isEmpty)
{
Companions[i].GetComponent<Companion>().objectToReach.GetComponent<Position>().SetEmptyOrNot(true);
Companions[i].GetComponent<Companion>().objectToReach = firstLine[j];
Companions[i].GetComponent<Companion>().distanceFromTarget = dist;
firstLine[j].GetComponent<Position>().SetEmptyOrNot(false);
}
}
}
}
}
并编辑了伴随脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Companion : MonoBehaviour
{
public GameObject objectToReach;
public float distanceFromTarget;
Transform target;
// Start function not needed
// Update is called once per frame
void Update()
{
target = objectToReach.transform;
//reach target
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), 1 * Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position, target.position, .01f);
}
}
问题是现在当一个位置被选择时,他们不再改变目标......
【问题讨论】:
标签: c# unity3d triggers artificial-intelligence game-engine