using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetMove : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.transform.position = RandomVector(0.1f, 0.5f);
}
private Vector3 RandomVector(float min, float max)
{
var x = Random.Range(min, max);
var y = Random.Range(min, max);
var z = Random.Range(min, max);
return new Vector3(transform.position.x + x, transform.position.y + y, transform.position.z);
}
}
[我希望它在0.1到0.5之间的小范围内随机移动,但是由于我这样做了+ x和+ y,所以它会不断改变位置,并且不停地移动得很远。
return new Vector3(transform.position.x + x, transform.position.y + y, transform.position.z);
我应该怎么做transform.position.x + x和transform.position.x + y?
这就是我想要的:该脚本已附加到多维数据集。科幻无人机上还有另一个脚本,我所要做的就是使无人机LookAt立方体。
就是这样,这会造成科幻无人机失控的失控效果。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetMove : MonoBehaviour
{
[SerializeField] private List<Vector3> waypoints = new List<Vector3>(); // Hom many items you want, will show in Inspector
public float speed;
private int current = 0;
private float WPradius = 1;
private void Start()
{
GetRandom();
}
private void Update()
{
MoveBetweenWaypoints();
}
private void GetRandom()
{
for (int i = 0; i < 30; i++)
{
waypoints.Add(new Vector3(Random.Range(transform.position.x, transform.position.x + 3f),
Random.Range(transform.position.y, transform.position.y + 3f),
transform.position.z));
}
}
private void MoveBetweenWaypoints()
{
if(Vector3.Distance(waypoints[current], transform.position) < WPradius)
{
current = Random.Range(0, waypoints.Count);
if(current >= waypoints.Count)
{
current = 0;
}
}
transform.position = Vector3.MoveTowards(transform.position, waypoints[current], Time.deltaTime * speed);
}
}