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 oj1999 · Aug 02, 2013 at 02:02 AM · playerenemyspawningwrong

Enemies spawning on player and rotated

I have a Spawn script that is working well except for two problems. I am attempting to spawn a ghost at a random position every few seconds. This part is working, but unfortunately it spawns them on their backs instead of right side up. I need them to rotate 90 degrees on the X axis and a cant figure out how to do it. Second sometimes the ghosts spawn on top of the player or very close. My goal is for them to not spawn unless they are a certain distance from my player. Here is my code:

 #pragma strict
 function Start(){
 yield WaitForSeconds(5);
    InvokeRepeating("Spawn",0.01f,3.0f);
 }
 var enemyPrefab: Rigidbody; 
 function Spawn(){
     for (var i = 0; i < Random.Range(1, 4); i++){
     var Pos = Vector3(Random.Range(-18,18), 2.3, Random.Range(-18, 18));//Where to spawn enemies
     var newEnemy: Rigidbody = Instantiate(enemyPrefab, Pos, Quaternion.identity);//Spawn
     newEnemy.tag = "ghost";//Tag the new enemy
 }
 }

Thanks for any help you can offer.

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 Hyperion · Aug 02, 2013 at 03:45 AM 0
Share

For the first problem, when you drag the prefab of the ghost into the scene, is it right side up?

avatar image oj1999 · Aug 02, 2013 at 05:18 AM 0
Share

no it is in the same position as when one is spawned. if I rotate the prefab, the clones do not retain the rotation

avatar image Twinny · Aug 02, 2013 at 06:01 AM 0
Share

im still new to Unity but it sounds like you are forgetting to hit the Apply button at the top of the inspector to save the changes made to the prefab?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by VioKyma · Aug 02, 2013 at 06:01 AM

For the spawn distance, I would do something like having a spawn distance variable to determine the distance from the player. You will also need the location of the player (obviously).

Then you will need to compare the pos variable with the player location before spawning.

 var spawnDistance = 5.0;
 var playerX = Player.transform.position.x;
 var playerZ = Player.transform.position.z;
 
 if ((pos.x < playerX - spawnDistance || pos.x > playerX + spawnDistance)
         && (pos.z < playerZ - spawnDistance || pos.z > playerZ + spawnDistance))
     // set the pos
 else
     // set some position that is appropriate

In this way, you can spawn an enemy so long as it's not within the specified range. And handle the case that it is by adjusting the pos vector.

For the rotation, you should be able to set LookAt() for the object after instantiating.

 newEnemy.tag = "ghost";//Tag the new enemy
 Vector3 myVector = new Vector3(x, y, z);
 newEnemy.transform.LookAt(myVector);

where x, y and z are the direction you are wanting it to face.

Hope this helps, let me know if you have any questions.

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

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

Kill the Player? 1 Answer

How to stop enemy within certain distance of player? 2 Answers

How to make Enemy on scary game ? 1 Answer

Player lives script 1 Answer

Enemy is not moving to player with A* pathfinding 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