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 chronicfail · Jun 07, 2013 at 06:22 AM · instantiateraycastcolliderrandom

Stop platforms spawning inside player

I am making a game where you play as a ball bouncing around randomly generated platforms. When one platform becomes invisible a new on will be Instantiated to replace it at a random point within the camera's view. This works but often they end up inside the player, sending them flying off randomly. Is there a way to make sure they do not appear anywhere within the player's collider? This is the code I am using. Also, if anyone knows how to only make them appear on the edge of the camera rectangle opposite the side the previous platform exited on, that would be helpful.

  ray1=Camera.main.ViewportPointToRay(Vector3(1,1,0));
  ray2=Camera.main.ViewportPointToRay(Vector3(0,0,0));
 
 var hit : RaycastHit;
 var hit2 : RaycastHit;
 
 if(Physics.Raycast(ray1,hit)){
 maxX=hit.point.x;
 minY=hit.point.y;
 }
 
 if(Physics.Raycast(ray2,hit2)){
 minX=hit2.point.x;
 maxY=hit2.point.y;
 }
 
 Rand1=Random.Range(minX,maxX);
 Rand2=Random.Range(minY,maxY);
 
 var platPos=Vector3(Rand1,Rand2,transform.position.z);
 var newPlat=Instantiate(platform,platPos,rot);
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 robertbu · Jun 07, 2013 at 07:18 AM

One way is just to repeatedly try until you get a position that works. This code assumes you setup a reference to the player game object called 'player'.

 var platPos : Vector3;
  
 do { 
     Rand1=Random.Range(minX,maxX);
     Rand2=Random.Range(minY,maxY);
     var platPos=Vector3(Rand1,Rand2,transform.position.z);
 } while (Vector3.Distance(platPos, player.transform.position) < some_value);
 
 var newPlat=Instantiate(platform,platPos,rot);

Another way would be to generate a position no closer than some distance to the player:

 var platPos : Vector3 = Random.insideUnitCircle;
 platPos = player.transform.position + platPos * some_distance;
 platPos.z = transform.z;

 var newPlat=Instantiate(platform,platPos,rot);
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 chronicfail · Jun 15, 2013 at 11:03 AM 0
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

14 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

Related Questions

Make sure objects are not instantiated on each other 2 Answers

how to move an object clone to mouse position, i dont understand ? 1 Answer

Find a random location, spawn an object there, do it again. 0 Answers

What would be a good way to not have objects spawn inside one another? 2 Answers

How to prevent objects from interpenetrating eachother ? 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