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 /
This question was closed Apr 20, 2013 at 08:26 PM by Eugenius for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Eugenius · Dec 28, 2012 at 09:56 AM · objectboundsplacementlimits

Placing an object with limits

Hi there.

I've used the mouse object placement found on the unity coder blog in order to help me instantiate some game objects whenever I want to but I appear to have an issue.

Does anyone know how I could add to the below code something like a limit or bounds around the gameobject that is instantiated? I am asking this because whenever I click on the item that is instantiated it instantiates another one which it allows me to move instead of simply allowing me to move the first one.

Thanks, Eugen

 #pragma strict
  
 public var ObjectToPlace:Transform; 
 private var clone:Transform;
  
 function Update (){
  
 if(Input.GetButtonDown("Fire1"))
 {
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 var layerMask = 1 << 8;
  
 
 if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask))
 {
 clone = hit.transform; 
  
 }else{ 
  
 layerMask = ~layerMask; 
 if (Physics.Raycast (ray, hit, Mathf.Infinity, layerMask))
 clone = Instantiate(ObjectToPlace, hit.point, Quaternion.identity);
 }
 }
  
 if(Input.GetButtonUp("Fire1")) 
 {
 clone=null; 
 }
  
 if(Input.GetButton("Fire1")) 
 {
 if (clone!=null) 
 {
 var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit2 : RaycastHit;
 var layerMask2 = 1 << 8;
 layerMask2 = ~layerMask2;
 if (Physics.Raycast (ray2, hit2, Mathf.Infinity, layerMask2)) 
 {
 clone.position = hit2.point;
 }
 }
 }
 }
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

  • Sort: 
avatar image
0
Best Answer

Answer by Eugenius · Jan 08, 2013 at 05:02 PM

Hello guys,

It took a while but I got it to work. The issue wasn't with this code actually but with the fact that I was using the object and instantiating it as it was instead of creating a prefab and instantiating.

It was the simplest solution I could figure out and I added a couple of boolean statements so that I can trigger the placement whenever I want.

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

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random Object Spawnning? 1 Answer

2D random object placement 0 Answers

Ease in transform.LookAt() - keep other target within camera boundaries 1 Answer

Default object placement location. 0 Answers

Object Placement on a slope. Any ideas? 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