mohammad
mohammad

Reputation: 2212

Unity 5.4 find the length of the object

hi every one i am using Unity 5.4.1f1 to create a simple game , i want my code to add platforms which is a piece of the street at the end of the first platform , with out hard coding the street length which is 14 m

so i tried this code :
i added in start :

lastPos = platForm.transform.position;
size = platForm.transform.localScale.z;

and added called this code in update :

Vector3 currentPos = lastPos;

currentPos.z +=size; lastPos =currentPos; Instantiate(platForm,lastPos,Quaternion.identity);

but it did't work it always get the size 1 even if it is 14 so is that doable in unity or i should do it hard coded ?

enter image description here

Upvotes: 1

Views: 1051

Answers (2)

Bizhan
Bizhan

Reputation: 17085

You can use renderer.bounds

For example

var size  = GetComponent<Renderer>().bounds.size;

Upvotes: 2

Gabriel Capeletti
Gabriel Capeletti

Reputation: 106

The scale represent a multiplier to the size of the object. So a object with 10m but with no scale will return the factor 1, if you duplicate the size of that model (20 m) the scale will be 2.

You can get the length of a mesh though its bounds

mesh.bounds.size, this vector3 will have a lengths for each axis.

https://docs.unity3d.com/ScriptReference/Mesh-bounds.html

Upvotes: 2

Related Questions