How to create random projectiles that seek targets (Csharp)

I am really new to unity and scripting in general so please be nice :slight_smile:

I am trying to create a game where asteroids are randomly spawned and fly towards targets and you shoot them with a turret. what i want to do is reverse my shooting script and make it spawn a ball away from the object then fly back at it with a range of inaccuracy.

this is my shooting code because i messed up my code past the point of understanding how it works

using UnityEngine;
using System.Collections;

public class Shoot : MonoBehaviour {
    public Rigidbody Bullet;
    public float shotSpeed = 10;
    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {
  
        if (Input.GetMouseButtonDown (0))
        {
            Rigidbody Bullet = Instantiate (Bullet, transform.position, transform.rotation) as Rigidbody;
            Bullet.velocity = transform.TransformDirection (new Vector3 (0, 0, shotSpeed));
        }
    }
}

have you looked through the “Space shooter” tutorial? it covers the basics on spawning “hazards” in random positions.

You would then only need to work out the necessary rotation to spawn the “hazard” to extend that to what you’ve briefly described.

You can use something like these

var dir = (playerTransform.position - enemyTransform.position).magnitude;

Bullet.velocity = dir * speed * time.deltaTime;

I just looked at that, now they are spawning in a random pattern but not moving, i tried to use the same add velocity code but now i get an error saying “object reference not set to an instance of an object” once the first asteroid spawned.

here is the code i ripped from the tutorial with my part in it

using UnityEngine;
using System.Collections;

public class Asteroids : MonoBehaviour
{
    public GameObject hazard;
    public Vector3 spawnValues;
    public int hazardCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;

    void Start ()
    {
        StartCoroutine (SpawnWaves ());
    }

    IEnumerator SpawnWaves ()
    {
        yield return new WaitForSeconds (startWait);
        while (true)
        {
            for (int i = 0; i < hazardCount; i++)
            {
                Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Rigidbody instantiatedhazard = Instantiate (hazard, spawnPosition, spawnRotation) as Rigidbody;
                instantiatedhazard.velocity = (new Vector3 (0, 0, 10));
                yield return new WaitForSeconds (spawnWait);
            }
            yield return new WaitForSeconds (waveWait);
        }
    }
}

[code]

which line of code is the error coming from (error has line number at the end, and if you double click on it it should take you to the line)?

what did you drag into the “hazard” slot, something from the scene hierarchy or a prefab from the project panel (should be the later)?

Does that hazard have a rigidbidy?

Yes it does

I have an issue where if I double click on it mono develop crashes but with this error it does not give me a line, instead it gives me a directory e.g cs28:assets/resources or something like that (I’m not at my computer right now so I can’t say what it is exactly but I remember there was no line number) it is set to a prefab called “asteroid 3” but there is also an instance of asteroid 3 when the game starts, that shouldent cause any problems right?