- Home /
Spawning Objects at location
Im trying to make my training dummies spawn in front of my player but instead they get spawned off the map how can i change their spawn position? This is a script i took from my lab at my uni.
using UnityEngine; using System.Collections;
public class TrainingDummySpawn : MonoBehaviour { public GameObject spawnedObject;
// Use this for initialization void Start () { Vector3 position=transform.position; for(int i=0;i<5;i++){ Instantiate(spawnedObject,position,Quaternion.identity); position.z+=5.0f; } } // Update is called once per frame void Update () { } }
Answer by greatestprez · Nov 24, 2012 at 01:15 AM
Try changing position.z+=5.0f to position.x+=5.0f. or lower the value
Answer by Frank Yeh · Nov 24, 2012 at 06:45 AM
Hi, please try below code
using UnityEngine;
using System.Collections;
public class SpawnTest : MonoBehaviour
{
public GameObject SpawnObject;
public float ForwardStep = 0.5f;
// Use this for initialization
void Start ()
{
for (int i = 0; i < 5; ++i)
{
GameObject tempGo = GameObject.Instantiate(SpawnObject, Vector3.zero, Quaternion.identity) as GameObject;
tempGo.transform.parent = transform;
tempGo.transform.localPosition = transform.forward * ForwardStep * (i + 1);
}
}
}
Answer by test84 · Nov 24, 2012 at 02:03 AM
You somehow have to find your player's position and then use that to spawn your enemies.
There are numerous ways to do this in Unity, read this: http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Your answer
Follow this Question
Related Questions
Trying out a unique method of spawning enemies. Someone want to help sort out the logic of it? 1 Answer
enemy wave spawn script wont work 0 Answers
delay first spawn 2 Answers
How do you create a "collect" objection that corresponds with the enemy behavior in C#? 1 Answer
Spawning different random objects at the same position? 2 Answers