有没有好的方法可以做到这一点,我已经复制了我的代码如下。这是我的代码,以便gameObjects在地图的特定区域周围巡逻,我需要一种方法,以便当敌人产生时,设置相对于产生敌人的gameObject的变换。
当我从预制件中产生我的敌人时,敌人应该在产生它的产卵点周围巡逻,但是我在游戏中有多个点来产生敌人。巡逻脚本有一个转换public Transform moveSpots;,我将产卵点对象分配给它。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Patrol0 : MonoBehaviour
{
public float speed;
public Transform moveSpots;
private float waitTime;
public float StartwaitTime;
public float MinX;
public float MaxX;
public float MinY;
public float MaxY;
void start()
{
moveSpots = GetComponentInParent<Transform>();
waitTime = StartwaitTime;
moveSpots.position = new Vector2(Random.Range(MinX, MaxX), Random.Range(MinY, MaxX));
}
private void Update()
{
transform.position = Vector2.MoveTowards(transform.position, moveSpots.position, speed * Time.deltaTime);
if (Vector2.Distance(transform.position, moveSpots.position) < 0.2f)
{
if (waitTime <= 0)
{
moveSpots.position = new Vector2(Random.Range(MinX, MaxX), Random.Range(MinY, MaxX));
waitTime = StartwaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
}
}
}发布于 2019-01-25 02:33:50
假设被派生的对象也是派生对象的父对象,您只需用transform.localPosition替换所有transform.position引用即可。如果没有,您可以使用从派生程序的转换中调用的Transform.TransformPoint()。
Transform Spawner;
void start()
{
moveSpots = GetComponentInParent<Transform>();
waitTime = StartwaitTime;
moveSpots.position = new Vector2(Random.Range(MinX, MaxX), Random.Range(MinY, MaxX));
moveSpots.position = Spawner.TransformPoint(moveSpots.position);
}这将获取局部空间moveSpots.position,并将其转化为相对于Spawner的世界空间。
https://stackoverflow.com/questions/54348534
复制相似问题