How do you instantiate an object halfway along an edge? So if I had a cube, how would I make an empty gameObject halfway along each of the edges?
Hi man, I think you need something along these lines. Basically you find the scale of the object on the given axis in world units, half it and then either add or subtract it to the transforms position:
var yScale = transform.lossyScale.y / 2;
xScale = transform.lossyScale.x / 2;
var topMiddlePosition = transform.position;
topMiddlePosition.y += yScale;
var bottomMiddlePosition = transform.position;
bottomMiddlePosition.y -=yScale;
var rightMiddlePosition = transform.position;
rightMiddlePosition.x += xScale;
var leftMiddlePosition = transform.position;
leftMiddlePosition.x -= xScale;
I’d tidy and stream line this a bit as I’ve put it in a very verbose format for the sake of comprehension.
Hope that helps!
Well, I have two solutions for that but they are not the perfect solutions that
calculate the edge size and find the half of the edge and put your stuff there also
I am not sure if it is efficient way or it is hardest way.
I have two solutions:
Solution 1:
From the editor check the exact place of the half of the edge and code it in mainobject such as:
var smallcube : GameObject;
function Start ()
{smallcube.transform.position.x=transform.localPosition.x+55(this is your variable);
}
Solution 2: Simply put empty gameobjects the the edges or whereever you want of
mainobject and make the child then equal the positions of the object you want, this
is recommended because the empty gameobjects size and moves with the main one and
you can configure if you need to. Hope I got you right and answered well.
OK, my solution was to go by size of the cube. This will only work with a cube, if you’ve got a rectangle you’ll need height, width and length instead of side length.
var myVerts : Vector3[];
var arr = new Array();
var vertice : Transform;
var position = Vector3.zero;
var lengthOfSide = 1.0;
var object : Transform;
function Start (){
/* var thisMatrix = transform.localToWorldMatrix; var vertices = GetComponent.<MeshFilter>().mesh.vertices;
for (vertex in vertices) {
print("mesh1 vertex at " + thisMatrix.MultiplyPoint3x4(vertex) );
arr.Push (vertex);
}
print (arr);
*/ //was used for something else, don't need this.
makeVert(lengthOfSide/2, lengthOfSide/2, 0);
//Here's the bit I haven't quite sorted...what happens if you want to do it on every edge? I don't want to write out makeVert (etc) 12 times, with slight changes each time. If anyone has an idea, please put it in a comment or answer.
}
function makeVert(xpos, ypos, zpos){
position = transform.position+Vector3(xpos,ypos,zpos);
print (position);
print (transform.position);
var vert = Instantiate (vertice, position, Quaternion.identity);
makeVert(lengthOfSide/2,lengthOfSide /2,0 );
}