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 fuyangli · Jan 08, 2013 at 01:31 PM · instantiateprefabspawnarea

Instantiate a Prefab with click in a certain area

First: I'm new here, and i'm not so good with english.

Ok. I already have a Script to spawn this prefab in a click. It's working perfect, but, I don't want to let the player spawn this prefab everywhere. Here's the code i'm using:

 function Build() { 
         var mousePos = Input.mousePosition;
         mousePos.z = Distance;
         var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
         objectPos.y = objectPos.y + distanciachao;
         Instantiate(Domino, objectPos, Quaternion.identity); 
 }

This make the prefab instatiate anywhere, but I Want to set a limit for this. I already have a box with the area I want for this Spawn. Any ideias?

Comment
Add comment · Show 1
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 DanielJF · Jan 08, 2013 at 01:37 PM 1
Share

$$anonymous$$aybe you can try with a raycaster that only reacts with your box so if you don't click on the box it just won't instantiate

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Karsnen_2 · Jan 08, 2013 at 02:43 PM

Gabriel, how about you try to load the required amount prefabs at the Start and Transform they to an Unknown location.

Then try to move one by one to the required whenever needed.

This way, if it works then it is good.

Else if it does not work then there is an issue with "objectPos". Also verify for the Y position on objectPos. It might take a lot of height putting your GObject up the roof & no where to see.

Let me know if it works or I hope it at least helps you to find the issue.

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 fuyangli · Jan 08, 2013 at 03:31 PM 1
Share

Hey $$anonymous$$arsnen, thanks for the reply!

I wanna make clear that the script I put there is what is already working in my game. It's working perfect, spawning the prefabs I want where I click. The next question is HOW to put a way to put a limit in the area where I can click to Instantiate that Prefab. Now it's like: Trace where the mouse is looking, then Instantiate a Prefab when there's a click. What I want: Trace where the mouse is IN A CERTAIN AREA, then Instantiate a Prefab wheren there's a click IN THAT SA$$anonymous$$E CERTAIN AREA.

Hope that explains more what I'm trying to do ^^

avatar image
0

Answer by plasticadmiral413 · Dec 14, 2017 at 10:24 AM

Put the instantiate function inside an "if" with a condition that only if mousePos is within a certain x & y. Your code will look somewhat like
function Build() { var mousePos = Input.mousePosition; mousePos.z = Distance; var objectPos = Camera.main.ScreenToWorldPoint(mousePos); objectPos.y = objectPos.y + distanciachao; if(mousePos.x < YOUR LIMIT FOR X && mousePos.y < YOUR LIMIT FOR Y) { Instantiate(Domino, objectPos, Quaternion.identity); } }

Comment
Add comment · 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

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

12 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

Related Questions

Error: Instantiated Enemies don't get hit 2 Answers

Spawning unique prefabs at different transform posititions 0 Answers

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer

Give prefab a parent 2 Answers

Instantiate Prefab 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