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 JayFitz91 · Nov 21, 2013 at 07:05 PM · spawncloneoffset

Spawn enemy at an offset

 var player: GameObject;
 var enemy : Transform;
 private var timer: float;
 
 var seenDistance = 40;
 var rotationSpeed = 100;
 var moveSpeed = 5;
 
 function Awake()
 {
     timer = Time.time + 10;
 }
 
 function Start () 
 {
     
 }
 
 function Update () 
 {
     
     var distance = Vector3.Distance(transform.position, player.transform.position);
     
     if(distance < seenDistance)
     {
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(player.transform.position - transform.position),rotationSpeed * Time.deltaTime);
         transform.position += transform.forward * moveSpeed * Time.deltaTime;
         
         if (timer < Time.time)
         {
             Instantiate(enemy, transform.position, transform.rotation);
             timer = Time.time + 10;
         }
     }
 }

This is the code for my enemy, i have it set up so that when the player gets within the distance of an enemy, it will start to spawn more and more enemies. This works the way i want.

What i need help with is the spawn function creates the enemy on top of the other enemy and as a result, the spawned enemy gets launched into the air for a short time.

Is there a way i can spawn the enemy to a slight offset of the enemy it's being created from?

Any replies appreciated.

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

2 Replies

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

Answer by Norcine · Nov 21, 2013 at 10:24 PM

The easiest way to do this is to add a simple offset to your class:

 var offsetX = 5;
 var offsetZ = 5;
 
 function Update()
 {
      // your stuff here up to the timer check
 
      if(timer < Time.time)
      {
            var offset = Vector3(Random.Range(-offsetX, offsetX), 0, Random.Range(-offsetZ, offsetZ));
            Instantiate(enemy, transform.position + offset, transform.rotation);
            timer = Time.time + 10;
      }
 }
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 JayFitz91 · Nov 21, 2013 at 10:49 PM 0
Share

Thanks man, works great

avatar image
1

Answer by paulygons · Nov 21, 2013 at 10:21 PM

I think you need this: http://docs.unity3d.com/Documentation/ScriptReference/Random-insideUnitCircle.html

Comment
Add comment · Show 2 · 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 Norcine · Nov 21, 2013 at 11:21 PM 0
Share

Nice, Pauly, I didn't know about that function.

avatar image paulygons · Nov 22, 2013 at 03:56 PM 0
Share

I hate it when I spend an hour writing a page long function that could have been done in one line!

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

How to have a cloned/duplicated enemy react the same way the original does? 1 Answer

Question about instantiate AI 0 Answers

When I pause the game the clone does not stop 0 Answers

Why I have a cloned prefab when spawning gameObjects? 0 Answers

ragdoll spawn flies up into air instead of staying still 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