i wonder what commannd to use to spawn objects when i click space on keyboard
i want to spawn cube
function Update() {
if (Input.GetButtonDown("Jump")) {
//??????????????????????????????
//what now??????????????????????
}
}
i wonder what commannd to use to spawn objects when i click space on keyboard
i want to spawn cube
function Update() {
if (Input.GetButtonDown("Jump")) {
//??????????????????????????????
//what now??????????????????????
}
}
Here is a sample for you using Instantiate. Note Instantiate uses prefabs. To make a Game Object Prefab just drag your game object to the Project window.
/////////////////////////////////////////////////////////////////////////////////
//
// SpawnZombies.cs
// Created: Darren Hedlund microcyb@gmail.com
//
// description: Spawn Zombie when player is near
//
// Note: Make an Empty GameObject and apply this script
// Uses Prefab GameObjects
/////////////////////////////////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;
public class SpawnZombies : MonoBehaviour
{
// player main camera tag as Player
private Transform _target;
// mutiple prefab objects
public Transform[] prefab;
// Minimum and Maximum number of prefabs
public int numMin;
public int numMax;
// Just shows how many were created
public int num;
// Minimum and Maximum placement of prefabs
public float xmin;
public float xmax;
public float ymin;
public float ymax;
private float rndNr;
private float tmprnd;
private int i = 0;
void Start()
{
// setup a random number of prefabs
num =Random.Range(numMin, numMax);
}
void Update()
{
// used to find the Player
GameObject _target = GameObject.FindGameObjectWithTag("Player");
// get the distance between the player and the Prefab GameObject
float dist = Vector3.Distance(_target.transform.position, this.transform.position);
// if prefab is far away then reset the spawn count
if (dist > 30f)
{
// int for the while loop
i = 0;
// setup a random number of prefabs
num =Random.Range(numMin, numMax);
}
// if within the distance between the Player and the GameObject
if (dist < 20f)
{
while (i < num)
{
// random pick of the Prefab you added to the array prefab
rndNr=Mathf.Ceil(Random.value*prefab.Length);
rndNr-=1;
// just to make sure, lets make it more random.
if(tmprnd==rndNr)
{
rndNr=Mathf.Ceil(Random.value*prefab.Length);
rndNr-=1;
}
// Instantiate uses prefabs. Generate the prefab Game object and place it a random position within the
// xmin,xmax & ymin&ymax
Object.Instantiate(prefab[(int)rndNr], new Vector3(transform.position.x+Random.Range(xmin, xmax), transform.position.y, transform.position.z+Random.Range(ymin, ymax)), transform.rotation);
tmprnd = rndNr;
i++;
}
}
}
}
[http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html][1] [1]: http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
– meat5000@meat5000 is right. Check out the documentation. It's a one liner to spawn a cube. var prefab : Transform; Instantiate (prefab, new Vector3(0,0,0), Quaternion.identity);
– jacobschellenbergAs alternative to Instantiate() look at [GameObject.CreatePrimitive()][1]: if (Input.GetKeyDown(KeyCode.Space)) { GameObject.CreatePrimitive(PrimitiveType.Cube); } [1]: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.CreatePrimitive.html
– robertbu