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 AsAnAsterisk · Jul 24, 2017 at 07:00 PM · scripting problemtransformenemyspawning

How to have an enemy spawn a certain distance behind the player

When a door opens, I want there to be a chance that an enemy will spawn behind the player, and while I can get the enemy to spawn, they usually do so beneath the map. I vaguely know what I need but have no idea how to apply it. Basically, I need to know how to spawn something a certain distance from a moving object, like the player, based on the direction they're looking.

    public class DoorSpawn : MonoBehaviour {
         public GameObject self;
         public GameObject player;
         public GameObject enemy1;
         public GameObject enemy2;
         public GameObject enemy3;
         Vector3 playerPos;
         Vector3 playerDirection;
         Vector3 spawnPos;
         Quaternion playerRotation;
     
         // Use this for initialization
         void Start () {
         Vector3 playerPos = player.transform.localPosition;
         Vector3 playerDirection = player.transform.forward;
         Quaternion playerRotation = player.transform.rotation;
         Vector3 spawnPos = playerPos;
         
         int randomValue = Random.Range (1,4);
             if (randomValue == 1) {
             enemy1.SetActive(true);
             enemy1Spawn();
                 if (enemy1.activeInHierarchy == true) {
                 self.SetActive(false);
                 }
             }
             if (randomValue == 2) {
             enemy2.SetActive(true);
             enemy2Spawn();
                 if (enemy2.activeInHierarchy == true) {
                 self.SetActive(false);
                 }
             }
             if (randomValue == 3) {
             enemy3.SetActive(true);
             enemy3Spawn();
                 if (enemy3.activeInHierarchy == true) {
                 self.SetActive(false);
                 }
             }
             if (randomValue == 4) {
             self.SetActive(false);
             }
         }
         
         // Update is called once per frame
         void Update () {
             
         }
         void enemy1Spawn() {
          enemy1.transform.position = spawnPos;
         
         }
         void enemy2Spawn() {
         enemy2.transform.position = spawnPos;
          
         }
         void enemy3Spawn() {
         enemy3.transform.position = spawnPos;
         
         }
     }
     
     
 
 
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
0

Answer by shadowpuppet · Jul 24, 2017 at 08:06 PM

The doors are probably in a fixed location therefor when the player opens it he will also be in a predictable location. Why not have an empty gameobject set somewhere behind the player and the door to use as a spawnpoint? A simple LookAt script on the spawnpoint to get the enemy to face the player.It will not be a smooth lookat but the player will be looking away and not see it happen

 public Transform target;//player goes here
 public GameObject badGuy;
 
 void OnTriggerEnter(Collider other)
     {
         if(other.gameObject.tag == "enemy")
         {
  badGuy.transform.LookAt(target);
Comment
Add comment · Show 1 · 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 AsAnAsterisk · Jul 24, 2017 at 09:21 PM 0
Share

I thought about doing that, however it wouldn't work, the area is very large, and there are many connections between rooms and hallways, so there are a lot of doors that can be first opened from either side. I appreciate your help all the same though!

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

120 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 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 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 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 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 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 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 avatar image

Related Questions

Correct way to spawn and move objects 1 Answer

Need enemy to follow only active character 0 Answers

NETWORK MANAGER SPAWN PREFAB NO SCRIPT 0 Answers

2 in 1 question - Enemy transforms and Frames per second 0 Answers

Trying to make a spawning enemy code 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