puting object in random position

hi guys,
i’m trying to put cube in diffrent position in scene, with random location, all over a floor cube…

i write this code to get scale of that floor, but it seem wrong:

function vScale(a:Transform):Vector3
{
var x:float= a.collider.size.x;
var y:float= a.collider.size.y;
var z:float= a.collider.size.z;
print(“”+x+" “+y+” "+z);
return Vector3(x,y,z);
}

i also tried lossyScale, localScale, GetComponent(“Meshfilter”).bound…
all of them return number ‘1’.

any help?

A bit of confusion… You a saying you want to put a cube on (or in) a floor with a random location, but you provided some code concerning the bounds of a collider. How are you using this to generate a floor plan? If it were a bunch of cubes that make up a floor, I could see it being a bit different in the end run.

ok…
the thing i concern, is the generated point, should be over floor cube… the width height, and start poisition of floor can be unknown… so i need to get position, and get position- (half of width) as start point and position+ half of width as end point and like this for other axis…

var getTransform:Transform;
function Start () {
var getInfo:Vector3=vScale(transform);
var x=Random.Range(getTransform.position.x-getInfo.x/2,getTransform.position.x+getInfo.x/2);
var z=Random.Range(getTransform.position.z-getInfo.z/2,getTransform.position.z+getInfo.z/2);
Debug.Log(getInfo.z);
Debug.Log(x+“-”+z);
transform.position=Vector3(x,10,z);
transform.rigidbody.AddForce(Vector3(0,0,0),ForceMode.Impulse);
}

function vScale(a:Transform):Vector3
{
var x:float= a.collider.size.x;
var y:float= a.collider.size.y;
var z:float= a.collider.size.z;
print(“”+x+" “+y+” "+z);
return Vector3(x,y,z);
}

I am sorry, I am still not following. Can you draw a picture or lay something out in Unity and get a screen shot to help us get the overall layout?

As much as I got is that you are trying to find a random position on a floor and put an object there. I do not know as of yet why you are focusing on the half distances.

because naturally, transform position x,y,z is center of an object, so if u want start and end of an object, you need to find it’s start -half it’s width and it’s start + half it’s width

Ah, I got it… yeah, its a math thing… You are comparing all of your positions to the center of the floorplan, you need to use the bounds of the floor plan.

Check out the modifications:

var getTransform:Transform;

function Start(){
	PositionOnFloor();
}

function Update(){
	if(Input.GetKeyDown("r"))PositionOnFloor();
}

function PositionOnFloor(){
	var myCollider : Vector3 = vScale(transform);
	var floorCollider : Vector3 = vScale(getTransform);
	var x = Random.Range(-(floorCollider.x - myCollider.x)/2, (floorCollider.x - myCollider.x)/2);
	var z = Random.Range(-(floorCollider.z - myCollider.z)/2, (floorCollider.z - myCollider.z)/2);
	transform.position=getTransform.position + Vector3(x,1, z);
}

function vScale(a:Transform):Vector3{
	var size=a.localScale;
	if(a.renderer)
		size=a.renderer.bounds.size;
	return size;
}

tnx man, so it’s renderer :slight_smile: