Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DavidDebnar · Oct 03, 2011 at 03:57 PM · instantiateenemyspawncorner

How can I spawn an enemy "behind the nearest corner"?

Hey guys! How can I instantiate a object at the nearest corner so he won't just appear from dust?

Comment
Add comment · Show 3
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 asafsitner · Oct 03, 2011 at 05:14 PM 0
Share

Well, you'll have to deter$$anonymous$$e what's the closest corner first, and then instantiate the enemy at a reasonable distance. But won't it be easier to place the enemy there and disable it, then place a trigger to activate it?

avatar image DavidDebnar · Oct 03, 2011 at 05:22 PM 0
Share

Thanks for the answer and no...It's littlebit complicated so I have to deter$$anonymous$$e what is the closest corner. But I do not know how.

avatar image asafsitner · Oct 03, 2011 at 05:29 PM 0
Share

How is your scene built? What's a corner? $$anonymous$$ore information means higher chance to get an answer.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Peter G · Oct 03, 2011 at 06:34 PM

It's too difficult to figure out what a corner is at runtime so you need to pre-mark all your corners. That is place empty game objects at all the possible spawn points. Then you can put them in an array and move through them easily.

 var spawnPoints : Transform[];
 var enemy : Transform;
 
 var player : Transform;
 
 function Start () {
      player = GameObject.FindWithTag("Player");
      InvokeRepeating("SpawnEnemy" , 5 , 5);
 }
 
 function SpawnEnemy () {
     var idealSpawn : Transform = spawnPoints[0];
     var curDist = Vector3.Distance(idealSpawn.position , player.position);
     //This is just a basic search algorithm.  We test every item in the array to see if it is better suited than the one's before it.
     //We assume that the first point is the best then we try and prove otherwise.
 
     for ( var i = 1 ; i < spawnPoints.Length ; i++) {
         //No need to test the first point.
         if (Vector3.Distance(spawnPoints[i].position , player.position) < curDist ) {
         //Are we closer than the old point?
              if( Physics.LineCast(player.position, spawnPoints[i].position) ) {
              //Is there something between the player and the point.
                 idealSpawn = spawnPoints[i];
                 curDist = var curDist = Vector3.Distance(idealSpawn.position , player.position);
              }
              else if ( Vector3.Dot( player.forward , (spawnPoints[i] - player.position).normalized ) < 0) {
              //or is the point behind the player?
                 idealSpawn = spawnPoints[i];
                 curDist = var curDist = Vector3.Distance(idealSpawn.position , player.position);
              }
        }
     }
     Instantiate (enemy , idealSpawn , Quaternion.Identity);
 }

This should pretty much do what you want. Let me just warn you about 2 oversights that I deliberately made:

  1. There are quicker ways of doing distance checks than with Vector3.Distance(). Its just the easiest to read so I put it in there and you can change it if you want.

  2. Possibly the more important one. This method breaks down if the character is closest to the "0" element in the array and he is already looking at it. It doesn't take that much effort to fix it. Just do a final check at the end to make sure that if the first element was the "ideal" spawn that it isn't visible.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Enemy not spawning correctly 1 Answer

Spawning different random objects at the same position? 2 Answers

How would I create a script that spawns objects more frequently as time goes on? 3 Answers

Error: Instantiated Enemies don't get hit 2 Answers

NullReferenceException: Instatiate with construtor OR in Inspector 1 Answer


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