- Home /
How do i spawn an enemy but with a randomized direction everytime it spawns?
I'm creating a topdown shooter but with the camera immovable.
i have a location (empty game object) im trying to spawn the enemy out of (which is in the center of the scene) i want them to spawn and translate outwards but everytime they spawn it's randomized in direction. i was working with a change in spawn time every time but couldn't get the rotation done as i have problem working with the "quarternion" section of the script? It confuses me how to work with that area.
Here is my script:
using UnityEngine; using System.Collections;
public class EnemySpawn : MonoBehaviour {
 public GameObject enemyToSpawn = null;
 
 public float spawnDelayMin = 5.0f;
 public float spawnDelayMax = 8.0f;
 
 private float _nextSpawnTime = 0.0f;
 private GameObject _spawnedEnemy = null;
 private Transform _t = null;
 
 // Use this for initialization
 void Start () 
 {
     _t = transform;
 }
 
 // Update is called once per frame
 void Update () {
     
     if(_spawnedEnemy == null && Time.time > _nextSpawnTime)
     {
         _nextSpawnTime = Time.time + Random.Range(spawnDelayMin, spawnDelayMax);
         
         _spawnedEnemy = Instantiate (enemyToSpawn, _t.position + new Vector3(Random.Range (-100,0,0), Quaternion.identity) as GameObject;
     }
 }
}
Answer by AlucardJay · Nov 25, 2013 at 07:02 AM
Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Euler.html
Check out UnityGems : http://unitygems.com/quaternions-rotations-part-1-c/
 _spawnedEnemy.transform.rotation = Quaternion.Euler( 0, Random.Range(0, 360), 0 );
maybe im understanding the use of quaternions incorrectly but i researched as it was linked, and ended up with an error of
Assets/Standard Assets/Scripts/Camera Scripts/Scripts/EnemySpawn.cs(29,39): error CS1061: Type UnityEngine.GameObject' does not contain a definition for rotation' and no extension method rotation' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
here is my script:
using UnityEngine; using System.Collections;
public class EnemySpawn : $$anonymous$$onoBehaviour {
 public GameObject enemyToSpawn = null;
 
 public float spawnDelay$$anonymous$$in = 5.0f;
 public float spawnDelay$$anonymous$$ax = 8.0f;
 
 private float _nextSpawnTime = 0.0f;
 private GameObject _spawnedEnemy = null;
 private Transform _t = null;
 
 // Use this for initialization
 void Start () 
 {
     _t = transform;
 }
 
 // Update is called once per frame
 void Update () {
     if(_spawnedEnemy == null && Time.time > _nextSpawnTime)
     {
         _nextSpawnTime = Time.time + Random.Range(spawnDelay$$anonymous$$in, spawnDelay$$anonymous$$ax);
         
         _spawnedEnemy = Instantiate (enemyToSpawn, _t.position, _spawnedEnemy.transform.rotation) as GameObject;
         
         _spawnedEnemy.rotation = Quaternion.Euler ( 0, Random.Range (0,360), 0) ;
     }
 }
}
i've been toying around with this for awhile and my belief is it has to do with the _spawnedEnemy.transform.rotation area of the code?
Sorry, _spawnedEnemy is a gameObject, so you have to access the transform component. I've edited the answer.
ooooh okay it's definitely working now! thanks alot!
Your answer
 
 
             Follow this Question
Related Questions
Problem with the enemy 0 Answers
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
Spawning Objects at location 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                