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 /
  • Help Room /
avatar image
0
Question by Kalicronic · May 08, 2019 at 10:44 AM · instantiatecollision detectionspawn points

(2D) Instantiating in front with offset

Hey

In front of my moving player GameObject (rigidbody2D) another GameObject shall be instantiated. But it should not collide with my player. This always means +-0.7 to +-2 units next to it. Here is my approach:

 Vector3 spawnPos = (player.transform.position + player.transform.forward) * spawnDistance;
 Vector3 playerWay = spawnPos;
 float distance = Vector2.Distance(spawnPos, playerWay);
 while ((distance < 0.7f) && (distance ) > 2f))
 {
    spawnPos.x = Random.Range(spawnPos.x - 2f, spawnPos.x + 2f);
    spawnPos.y = Random.Range(spawnPos.y - 2f, spawnPos.y + 2f);
    distance = Vector2.Distance(spawnPos, playerWay);
 }
 Instantiate(enemy, spawnPos, Quaternion.identity, transform);

I think my approach with x and y is not correct. It may only move the spawn point to the left and right.

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 Hellium · May 08, 2019 at 10:57 AM

 float minDistance = 0.7f;
 float maxDistance = 2f;
 float distance = Random.Range( minDistance, maxDistance );
 if( Random.value < 0.5f ) distance = -distance;
 Vector3 spawnPos = (player.transform.position + player.transform.up) * spawnDistance + player.transform.right * distance;
 Instantiate(enemy, spawnPos, Quaternion.identity, transform);
Comment
Add comment · Show 4 · 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 Kalicronic · May 08, 2019 at 11:09 AM 0
Share

@Hellium That's not what I'm looking for. Here is a sketched example: alt text

spawnlogic.png (11.5 kB)
avatar image Hellium Kalicronic · May 08, 2019 at 11:38 AM 0
Share

This drawing would have been very useful in your original question.

I've update my answer, give it a try.

avatar image Kalicronic · May 08, 2019 at 02:28 PM 0
Share

@Hellium All right, we're getting closer to the solution. If this was not yet understandable, the player can move in any direction from (2D). I tried to replace the .up with .forward in your solution. But the spawnPos is sometimes out of my range.

Sketches correction: alt text

spawnlogic.png (13.2 kB)
avatar image Hellium Kalicronic · May 09, 2019 at 07:28 AM 0
Share

What does not work with the provided solution?

If you want the enemies to be spawn on the same "axis" (only on the Y axis for instance, regardless of the player rotation, but aligned with player position), try

  Vector3 spawnPos = (player.transform.position + Vector3.up) * spawnDistance + Vector3.right * distance;

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

195 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 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

Check if a large number of gameobjects are colliding 1 Answer

Instantiate object during collision only once 1 Answer

Instantiating from different Sources causes collision inconsistency 0 Answers

How can I detect only 1 collision between two clones of the same prefab? 0 Answers

Which of Mulitipe colliders OnCollisionEnter ? 0 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