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 BenjaminC · May 14, 2013 at 01:28 PM · gameobjectraycastmeshvectrosityscreenpointtoray

Dynamic mesh positioning with Raycast intersection...

I'm very new to programming in Unity, so i'm sure i've made several fundemental errors in how i've approached this problem.

I'm attempting to select elements on screen using a 2d selection box in an RTS style. I've used Vectrosity to create the box, and then create rays to determine where the box intersects the ground plane. I then use these Rays to get Vector3's of their intersection with another plane. This is then used to create a mesh which is then applied to another GameObject. I then hope to use this GO to collision detect to find out what's selected.

Now, i'm sure this is a bad way of doing it, in fairness this is really just an exercise to learn more about how this stuff works. However I cannot get it to work. I'm close, but the Mesh I create seems to position the GO it's applied to in a very strange place.

Now i've messed around with translating the position etc. but to no avail. I'm wondering if someone could shed some light onto this for me - I'd appreciate it greatly.

Also, if anyone has a better way of doing this i'd be very interested to hear. Ultimately though I'd still like to determine what i'm doing wrong in my attempt!

Many thanks in advance kind folk. - B

Source:

 #pragma strict
 
 import Vectrosity;
 
 var hitPlane : Plane;
 var lineMaterial : Material;
 var textureScale = 4.0;
 var mainCamera : Camera;
 
 
 var tl : Ray;
 var tr : Ray;
 var br : Ray;
 var bl : Ray;
 
 
 private var selectionLine : VectorLine;
 private var originalPos : Vector2;
 
 function Start () {
     var groundgo : GameObject = GameObject.Find("Ground");
     hitPlane = new Plane(Vector3.up, Vector3.zero);
     VectorLine.SetCamera(camera);
     selectionLine = new VectorLine("Selection", new Vector2[5], lineMaterial, 3.0, LineType.Continuous);
 }
 
 
 function Update () {
     
     if (Input.GetMouseButtonDown(0)) {
         selectionLine.active = true;
         originalPos = Input.mousePosition;
     
     }
     
     if (Input.GetMouseButton(0)) {
         selectionLine.MakeRect (originalPos, Input.mousePosition);
         selectionLine.Draw();
     }
     
     if (Input.GetMouseButtonUp(0)) {
         selectionLine.active = false;
         DoSelection();
     }
     
 
     tl = mainCamera.ScreenPointToRay( selectionLine.points2[0] );
     tr = mainCamera.ScreenPointToRay( selectionLine.points2[1] );
     br = mainCamera.ScreenPointToRay( selectionLine.points2[2] );
     bl = mainCamera.ScreenPointToRay( selectionLine.points2[3] );
     
 
     Debug.DrawRay ( tl.origin, tl.direction * 100, Color.red);
     Debug.DrawRay ( tr.origin, tr.direction * 100, Color.red);
     Debug.DrawRay ( br.origin, br.direction * 100, Color.red);
        Debug.DrawRay ( bl.origin, bl.direction * 100, Color.red);
     
         
 }
 
 function DoSelection() {
 
         var distance : float = 1;
         var groundgo : GameObject = GameObject.Find("Ground");
         
 
         //Debug.Log(ground);
         
         if(hitPlane.Raycast(tl, distance)) {
             var pointtl:Vector3 = tl.GetPoint(distance);
             //pointtl = ConvertAxes(mainCamera.ScreenToWorldPoint(pointtl));
         }
         
         if(hitPlane.Raycast(tr, distance)) {
             var pointtr:Vector3 = tr.GetPoint(distance);
             //pointtr = ConvertAxes(mainCamera.ScreenToWorldPoint(pointtr));
         }
         
         if(hitPlane.Raycast(bl, distance)) {
             var pointbl:Vector3 = bl.GetPoint(distance);
             //pointbl = ConvertAxes(mainCamera.ScreenToWorldPoint(pointbl));
         }
         
         if(hitPlane.Raycast(br, distance)) {
             var pointbr:Vector3 = br.GetPoint(distance);
             //pointbr = ConvertAxes(mainCamera.ScreenToWorldPoint(pointbr));
         }
         
 
         var mesh = new Mesh();
         var vertices = Array(pointtl, pointtr, pointbl, pointbr);
         var tri: int[] = new int[6];
 
         
         tri[0] = 0;
         tri[1] = 2;
         tri[2] = 1;
         tri[3] = 2;
         tri[4] = 3;
         tri[5] = 1;
         
         mesh.vertices = vertices;
         mesh.triangles = tri;
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
         mesh.Optimize();
 
         var go:GameObject = GameObject.Find("HitPlane");
         
         
         go.GetComponent(MeshFilter).mesh = mesh;
         go.GetComponent(MeshCollider).mesh = mesh;
         
         
         
 }
 
 
 function ConvertAxes(point:Vector3) {
     var rpoint : Vector3 = Vector3(point[0], 0, point[2]);
     return rpoint;
 }

(edit: tidied up my messy code a bit!)

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

0 Replies

· Add your reply
  • Sort: 

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

Raycast against sub mesh of GameObject 0 Answers

Mesh Collider Issue(?) - Raycast (ScreenPointToRay) Appears to Collide on Nothing 0 Answers

How to have different materials on one GameObject? 1 Answer

How to show 3000+ Trees(generator) without getting Memory overflow (no terrain generator) 0 Answers

Move up gameobject from under the ground or object (vibration problem) 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