I have a script that gets 4 rays based off of the Mouse down point and the mouse up point on the screen.
(Box group selection)
I use the Command camera.ScreenPointToRay to generate the first 4 verticies of the box, and use the directions of the ray to determine the last 4 points of the box.
For some reason The box’s vertexes are inaccurate in when compared to the camera’s position.
However, It DOES work when the camera is at position 0,0,0, and 0,0,0 rotation.
For those that wish to test it themselves you just need a blank Project scene and attach this script to the main camera. And one prefab with only a game object. Must assign it to the box field on the script on the camera.
var box : GameObject;
private var CrnFirst : Vector3;
private var Crnend : Vector3;
private var Hal : GameObject;
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
CrnFirst = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
Crnend = Input.mousePosition;
//print("Spot 1: " + CrnFirst + "Spot 2: " + Crnend);
//print(camera.ScreenPointToRay(CrnFirst));
SelectionCube();
}
}
function SelectionCube()
{
Hal = Instantiate (box,transform.position,transform.rotation);
Hal.AddComponent("MeshFilter");
Hal.AddComponent("MeshRenderer");
var mesh : Mesh = Hal.GetComponent(MeshFilter).mesh;
var rays : Ray[];
rays = new Ray[4];
rays[0] = camera.ScreenPointToRay(CrnFirst);
rays[1] = camera.ScreenPointToRay(Vector3(Crnend.x, CrnFirst.y));
rays[2] = camera.ScreenPointToRay(Crnend);
rays[3] = camera.ScreenPointToRay(Vector3( CrnFirst.x,Crnend.y));
print(rays[0]);
var size = 1;
var vertexList = [
rays[3].origin,
rays[0].origin,
(rays[0].origin+(rays[0].direction*500)),
(rays[3].origin+(rays[3].direction*500)),
(rays[1].origin+(rays[2].direction*500)),
(rays[1].origin+(rays[1].direction*500)),
rays[1].origin,
rays[2].origin
];
mesh.vertices = vertexList;
var faceList = [
2,1,0,
3,2,0,
//0, 1, 2, // 1: face arrière
//0, 2, 3,
// 3, 2, 5, // 2: face droite
//3, 5, 4,
5,2,3,
4,5,3,
//5, 2, 1, // 3: face dessue
//5, 1, 6,
1,2,5,
6,1,5,
//3, 4, 7, // 4: face dessous
//3, 7, 0,
7,4,3,
0,7,3,
//0, 7, 6, // 5: face gauche
//0, 6, 1,
6,7,0,
1,6,0,
//4, 5, 6, // 6: face avant
//4, 6, 7
6,5,4,
7,6,4
];
mesh.vertices = vertexList;
mesh.triangles = faceList;
mesh.RecalculateNormals();
}