- Home /
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!)
Your answer
Follow this Question
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