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
1
Question by Mentaner · Aug 07, 2014 at 05:36 PM · raycastprefabsareaselectfill

How to fill selected area with prefabs?

Hello, I am making a 3D top-down game.

I have a JS script that allows me to drag a texture like a selection box in an RTS game. At the end of the selection I want to fill the selected area automatically with prefabs.

I just dont know how..

 private var mouseButton1DownPoint : Vector2;
 private var mouseButton1UpPoint : Vector2;
 private var mouseButton2DownPoint : Vector2;
 private var mouseButton2UpPoint : Vector2;
 private var mouseLeftDrag : boolean = false;
 private var raycastLength : float = 200.0;
 
 // semi transparent texture for the selection rectangle
 var selectionTexture : Texture;
 // floor Spawner
 var ConstGrid : GameObject;
 
 // range in which a mouse down and mouse up event will be treated as "the same location" on the map.
 private var mouseButtonReleaseBlurRange : int = 20;
 
 function OnGUI() {
 if (mouseLeftDrag) {
 
 var width : int = mouseButton1UpPoint.x - mouseButton1DownPoint.x;
 var height : int = (Screen.height - mouseButton1UpPoint.y) - (Screen.height - mouseButton1DownPoint.y);
 var rect : Rect = Rect(mouseButton1DownPoint.x, Screen.height - mouseButton1DownPoint.y, width, height);
 GUI.DrawTexture (rect, selectionTexture, ScaleMode.StretchToFill, true);
 }
 }
 
 function Update ()
 {
 // Left mouse button
 if (Input.GetButtonDown("Fire1")) {
 Mouse1Down(Input.mousePosition);
 }
 
 if (Input.GetButtonUp("Fire1")) {
 Mouse1Up(Input.mousePosition);
 }
 
 if (Input.GetButton("Fire1")) {
 // Used to determine if there is some mouse drag operation going on.
 Mouse1DownDrag(Input.mousePosition);
 }
 
 //Right mouse button
 if (Input.GetButtonDown("Fire2")) {
 Destroy(gameObject);
 }
 
 }
 
 function Mouse1DownDrag(screenPosition : Vector2) {
 // Only show the drag selection texture if the mouse has been moved and not if the user made only a single left mouse click
 if (screenPosition != mouseButton1DownPoint) {
 mouseLeftDrag = true;
 // while dragging, update the current mouse pos for the selection rectangle.
 mouseButton1UpPoint = screenPosition;
 
 var hit : RaycastHit;
 ray = Camera.main.ScreenPointToRay (screenPosition);
 if ( Physics.Raycast (ray, hit, raycastLength) )
 {
 selectionPointEnd = hit.point;
 }    
 
 }
 }
 
 function Mouse1Down(screenPosition : Vector2) {
 
 mouseButton1DownPoint = screenPosition;
 
 var hit : RaycastHit;
 var ray = Camera.main.ScreenPointToRay (mouseButton1DownPoint);
 
 if ( Physics.Raycast (ray, hit, raycastLength))
 {
 
 mouseButton1DownTerrainHitPoint = hit.point;
 selectionPointStart = hit.point;
 }
 }
 
 function Mouse1Up(screenPosition : Vector2) {
 
 mouseButton1UpPoint = screenPosition;
 var hit : RaycastHit;
 
 mouseLeftDrag = false;
 }

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 erick_weil · Aug 07, 2014 at 07:41 PM

ok, i did'nt uderstanded well your code, but to fill a retangle with prefabs knowing his start point and end point is simple:

 start._____________
       !            !
       !            !
       !            !
       !            !
       !____________!. end

if your prefabs are circles with a diameter of 1 unit, you only need to arrange two loops and put then inside the retangle:

 function retangule (StartPoint : Vector3,EndPoint : Vector3) {
 var Retangle : Vector3;
 Retangle = EndPoint - StartPoint;
 var width : int = Retangle.x/radius;
 var height : int = Retangle.z/radius;
 var d : Vector2 = Vector2(1,1);
 if( width <0)
 {
 width = -width;
 d.x = -1;
 }
 if( height <0)
 {
 height = -height;
 d.y = -1;
 }
 for(var x=0;x<width;x++)
 {
 for(var z=0;z<height;z++)
 {
 var px : float = StartPoint.x+(x*radius*d.x);
 var pz : float = StartPoint.z+(z*radius*d.y);
 Instantiate(prefab,Vector3(px,0,pz),prefab.transform.rotation);
 }
 }
 }
     
     

}

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 Mentaner · Aug 07, 2014 at 09:20 PM 0
Share

thanks for your help!

It's probably just me ... but I can't get your script to run. I have changed the axes because for me Z is responsible for height and I also don't get the radius idea... is it to get the prefab scale ?

avatar image erick_weil · Aug 08, 2014 at 11:34 PM 0
Share

the radius is the prefab scale, then must be greather than zero and smaller than the given area, are you using unity2D? i tested this in unity3d setup, you need to remember to call the function giving the start and endpoint that make enought space to fit at least one prefab with the given "radius".

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycasting a specific square area 1 Answer

Bottom area of a 3D model? Raycast? 1 Answer

Select instantiated object with mouse 1 Answer

C# mouse Raycast question 3 Answers

Move selected object. 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