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 tss · Mar 08, 2015 at 03:22 AM · javascriptpositionvector3spawnbeginner

Enemy spawn script/player position(java/js)

I am working on an enemy spawn script that needs some adjustments/polishing. I want to make zombies spawn in front of my "guy/player". After they spawn in front of him, I know how to do the rest. Just the spawning in front part is the only thing I am confused on. I was using a Invoke repeating() to make zombies spawn at a certain spot but now I want them to span directly in front of my player.I know that I will have to get my "players" position but I don't know what to do from there. Thanks for the help and your time.(feel free to rewrite my code or make any helpful adjustments)

here is my code:

 var SpawnObject : GameObject;  
    var player : Transform;
    private var SpawnStartDelay : float = 0; 
    var SpawnRate : float = 1.0; 
    private var amountOfE : float = 0;
    var maxAmount : float = 10; 
    var distance = 15; 
    var playerCloseEnough = false;
    
    function Start() 
    {
        InvokeRepeating("Spawn", SpawnStartDelay, SpawnRate); 
    } 
    
    // Spawn the SpawnObject 
    function Spawn() 
    {
        if(playerCloseEnough == true)
        {
        amountOfE++;
        Instantiate(SpawnObject, transform.position, transform.rotation);
        }
        else{}
    }
   
    function Update()
    {
        if(amountOfE == maxAmount)
            {
                 CancelInvoke("Spawn");
                 Destroy(this.gameObject);
            }
            else{}
            if(Vector3.Distance(transform.position, player.position) < distance)
            { 
                playerCloseEnough = true; 
            } 
    }

   


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
Best Answer

Answer by S_Darkwell · Mar 08, 2015 at 03:33 AM

Try replacing:

 Instantiate(SpawnObject, transform.position, transform.rotation);

With:

 Instantiate(SpawnObject, transform.position + (transform.forward * 2.0f), transform.rotation);

Replace "2.0f" with the distance you want them to be spawned in front of the player.

Alternatively, you can use:

 Instantiate(SpawnObject, transform.position + transform.TransformVector( 0f, 0.5f, 2.0f ), transform.rotation);

Replace with "0f, 0.5f, 2.0f" with either x,y,z floats or a Vector3 representing the position you wish to spawn the enemy in relation to the direction that the player is facing.

Hope that helps!

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 tss · Mar 08, 2015 at 05:48 PM 0
Share

Do I then apply this script to the player, or my enemies.

avatar image S_Darkwell · Mar 08, 2015 at 09:29 PM 0
Share

As I wrote it, it would have to be applied to the player script.

If you wanted to add it to the enemy script ins$$anonymous$$d, you would have to retrieve the player's transform first, then apply this code.

.TransformVector(#,#,#) applies to whatever transform precedes it. For example, if you had a variable named PlayerChar in your enemy script that contained your player's GameObject, you could call:

 PlayerChar.transform.position + PlayerChar.transform.TransformVector( 0f, 0.5f, 2.0f );
avatar image tss · Mar 14, 2015 at 03:06 PM 1
Share

Thank you.

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

22 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

Related Questions

Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer

Vector3 Transform.Position Not Working 2 Answers

JS access spherecast position 1 Answer

Vector2 Error when Declaring new Vector3 1 Answer

Monster Spawner 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