Is there a good way to do this, i have copied my code below. This is my code so that the gameObjects patrol around a certain area of a map, i need a way so that when the enemies are spawned in to set the transform relative to the gameObject spawning the enemy in.
When I spawn my enemy in from the prefab, the enemy should patrol around the spawn point which spawned it in, however i have multiple points within the game which spawns the enemies in. The Patrol script has a transform public Transform moveSpots; which i assign the spawn point objects to.
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;
}
}
}
}
parentObject.transform.SetParent(childObject.transform);? - derHugo