How do I get randomly generated piranhas to move to the propeller? UNITY C#

Asked

Viewed 43 times

-3

Well I need randomly created piranhas to move towards the helicopter and in contact cause a damage (reducing the player’s life).

this is the Spawn code with random location.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class piranhaSpawn : MonoBehaviour
{

    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);
            Instantiate(piranhaMon, new Vector3(xPos, yPos, 0), Quaternion.identity);
            yield return new WaitForSeconds(0.6f);
            piranhaCount += 1;
            
        }
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

  • 1

    In his Update() you shouldn’t move them?

1 answer

1

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.