Moid tera3, to make one gameobject move to another you can create a script and assign to each piranha, in it you reference the helice and use the function Movetowards to make it go to the object. If it is a 2D game (from what I understand from the code, I think it is) you can look at the function to Vector2 of the Movetowards, but the idea is essentially the same.
I took this code from Unity’s own documentation and adapted it.
public class Piranha : MonoBehaviour
{
public Vector2 HeliceTransform;
private float speed = 10.0f;
private Vector2 target;
void Update()
{
float step = speed * Time.deltaTime;
// move sprite towards the target location
transform.position =
Vector2.MoveTowards(transform.position, HeliceTransform.position, step);
}
}
In case the Helicetransform is a public property then you can do the following.
Take the propeller’s Transform reference in your Spawn code and store the reference of the piranha instance and then take the Piranha component and assign the propeller’s Transform. Thus:
public class piranhaSpawn : MonoBehaviour
{
public Transform HeliceTransform; // REFERENCIA PARA A HELICE
public GameObject piranhaMon;
public float xPos;
public float yPos;
public int piranhaCount;
void Start()
{
StartCoroutine(PiranhaSpawn());
}
IEnumerator PiranhaSpawn() {
while (piranhaCount < 6)
{
xPos = Random.Range(7.2f, 13f);
yPos = Random.Range(-3f, 3f);
GameObject piranhaObj; // ARMAZENA A INSTANCIA DA PIRANHA NESSA VAR
piranhaObj = Instantiate(piranhaMon, new Vector3(xPos, yPos, 0), Quaternion.identity);
// ATRIBUI O VALOR DO TRANSFORM HELICE NA PIRANHA
piranhaObj.GetComponent<Piranha>().HeliceTransform = HeliceTransform;
yield return new WaitForSeconds(0.6f);
piranhaCount += 1;
}
}
}
The codes may need a revision, but that’s the idea. I hope I gave you a north and helped in some way.
I strongly recommend that you do a tutorial for beginners, there are several free videos on youtube teaching or if you already have knowledge in programming, take a look at documentation whenever in doubt.
In his
Update()
you shouldn’t move them?– Leandro Angelo