Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by travisdreams · Nov 25, 2013 at 06:47 AM · enemyspawn

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;
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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 );
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image travisdreams · Dec 02, 2013 at 05:00 AM 0
Share

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?

avatar image AlucardJay · Dec 02, 2013 at 05:25 AM 0
Share

Sorry, _spawnedEnemy is a gameObject, so you have to access the transform component. I've edited the answer.

avatar image travisdreams · Dec 02, 2013 at 06:10 AM 0
Share

ooooh okay it's definitely working now! thanks alot!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges