Spawning elements in Unity on Button Click

Spawning elements in front of player on Button Click in Unity3D.

In this tutorial I am going to show you how to spawn elements in front of player.

using UnityEngine;

public class SpawnObject : MonoBehaviour {

    public GameObject cube;

    void Update () {

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            Instantiate(cube, new Vector3(0, 0, 0), Quaternion.identity);
        }

    }
}

This script above is going to spawn an object on position (0,0,0) in the scene after pressing the KeyNumber 1 on the keyboard.

In our example it is the Cube Object.

Now we are going a step further and we are going to spawn the object in front of player.

If we got the SpawnObject script attached to our player we can achieve it with this script:

using UnityEngine;

public class SpawnObject : MonoBehaviour {

    public GameObject cube;
    public float distanceFromPlayer = 4f;

    void Update () {

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            Instantiate(cube, transform.position + (transform.forward * distanceFromPlayer), Quaternion.identity);
        }
    }
}

We get the position of the player and the direction it is facing. We multiply it with the distance in which we want it to spawn and we get our spawning script ready.

What we can also do is add more objects and add Rigidbody element to those objects.

using UnityEngine;

public class SpawnObject : MonoBehaviour {

    public GameObject cubeSmall;
    public GameObject cubeBig;
    public GameObject sphereSmall;
    public GameObject sphereBig;

    public float distanceFromPlayer = 4f;

    void Update () {

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            Instantiate(cubeSmall, transform.position + (transform.forward * distanceFromPlayer), Quaternion.identity);
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            Instantiate(cubeBig, transform.position + (transform.forward * distanceFromPlayer), Quaternion.identity);
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            Instantiate(sphereSmall, transform.position + (transform.forward * distanceFromPlayer), Quaternion.identity);
        }
        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            Instantiate(sphereBig, transform.position + (transform.forward * distanceFromPlayer), Quaternion.identity);
        }
    }
}

This way we get some Physics to our Objects.

If you want to spawn the objects from the sky, you could modify the script to look like this:

using UnityEngine;

public class SpawnObject : MonoBehaviour {

    public GameObject cubeSmall;
    public GameObject cubeBig;
    public GameObject sphereSmall;
    public GameObject sphereBig;

    public float distanceFromPlayer = 4f;
    public float heightOfSpawn = 5f;
    void Update () {

        Vector3 spawningObjectsFromSky = transform.position + (transform.forward * distanceFromPlayer);
        spawningObjectsFromSky.y = spawningObjectsFromSky.y + heightOfSpawn;

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            Instantiate(cubeSmall, spawningObjectsFromSky, Quaternion.identity);
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            Instantiate(cubeBig, spawningObjectsFromSky, Quaternion.identity);
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            Instantiate(sphereSmall, spawningObjectsFromSky, Quaternion.identity);
        }
        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            Instantiate(sphereBig, spawningObjectsFromSky, Quaternion.identity);
        }
    }
}

As you see, we extracted the spawning position and changed the “y” position value. We made the height Of Spawn public, because this way we can modify it from the Inspector.

When we change the Height of Spawn to 10, in our example it gonna look like it is spawning from the sky:

And that’s all for today. I hope you learned something and that i did helped you with your project.

Here are other tutorials in which you could be interested in:

Making the camera follow an Object in Unity3D

How to turn the Bulb light On with Script in Unity3D

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.