spawning object at random defined points

hello

I want that an object is instantiated when i press Q
right now i have this for that

using UnityEngine;
using System.Collections;

public class kleidoufschieten : MonoBehaviour {
   
    public Rigidbody projectile;

    // Use this for initialization
    void Start ()
    {
    }
   
    // Update is called once per frame
    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.Q))
         {
            Vector3 euler = transform.eulerAngles;
            euler.x = 330 + Random.Range(0, 3) * 5;
            transform.eulerAngles = euler;
            Rigidbody clone;
            clone = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
            clone.velocity = transform.TransformDirection(Vector3.forward * 24);

        }
    }
}

this script is on the emty gameobject where the projectile shoots from. When i press Q it first rotates the object on the x axes and then shoots it.
But now i want to add another empty gameobject where it can shoot from. so when i press Q it picks an object random and then shoots that from it. So for example one empty gameobject is from the left and one is from the right.
That when i press Q it needs to sometimes do left right right left etc.
but it also needs to give it a rotatoin so the:

Vector3 euler = transform.eulerAngles;
            euler.x = 330 + Random.Range(0, 3) * 5;

but how do i do that?
hope someone can help

put the script on a parent empty gameobject, the “manager” of this behaviour. Add children to the parent which represent the spawn points to use in the scene.

In Start() do

... = GetComponentsInChildren<Transform>();

(or if you want create a blank class script for the spawn points and search for that type rather than Transform :slight_smile: )
to pick up all the points and put it into an array/list/whatever collection. Then look up Random.Range and use that to pick a random spawn in the collection.

yeah i understand this but i dont really now how to do it in code?

void Start()
{
    Transform[] spawnPoints = GetComponentsInChildren<Transform>();
}

void Update()
{
    if(Input.GetKeyDown(KeyCode.Q)))
    {
        chosenSpawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length - 1)];
     
        // rest of code
        // use "chosenSpawnPoint.position" rather than "transform.position"
        // use "chosenSpawnPoint.rotation" rather than "transform.rotation"
    }
}

(not checked for errors :stuck_out_tongue: )

when you put the empty game objects into the scene make sure they are rotated correctly so when you use their rotation they go left/right as you want

1 Like

that worked thanks