- Home /
Align a very easy cube mesh with collider to a Line!?
Hey Guys,
id like to align a cube to a line. For example: i draw a line with x0/y0 and x1/y1.... Now i wanted to align a cube to this line. It should have the same size, the same rotation and the same location.
How could i do this?
thanks
Answer by Elharter · Feb 08, 2018 at 01:07 PM
i got 1 line. with a start coordinaten and an end-coordinte (x0/y0 and x1/y1). And id like to align a cube (for example) to this line. With size, length, rotation....
I solved it partially....
//set line ends to vectors
Vector3 vecA = new Vector3(data.line[i].x, 40, data.line[i].y);
Vector3 vecB = new Vector3(data.line[i].x1, 40, data.line[i].y1);
//get our midway point
Vector3 midPointAB = (vecA + vecB) / 2f;
//get the direction between the two transforms -->
Vector3 dirAB = (vecB - vecA).normalized;
// get the rotation of the line
//Vector3 relativePos = vecA - transform.position;
Quaternion rotationAB = Quaternion.LookRotation(dirAB);
// get the length of the line
Vector3 abLength = vecB - vecA;
line.transform.localPosition = midPointAB;
line.transform.rotation = rotationAB;
// set the new length (scaling size from cube prefab is 1/80/1/)
line.transform.localScale = new Vector3(line.transform.localScale.x, line.transform.localScale.y, abLength.z);
But it doesnt work perfectly because the scaling is wrong, in relation to the direction. So i try since 10minutes with magnitude to solve it.
Solved it !
//set line ends to vectors Vector3 vecA = new Vector3(data.lines[i].x, 40, data.lines[i].y); Vector3 vecB = new Vector3(data.lines[i].x1, 40, data.lines[i].y1); //get our midway point Vector3 midPointAB = (vecA + vecB) / 2f; //get the direction between the two transforms, normalized Vector3 dirAB = (vecB - vecA).normalized; // get the length of the line Vector3 abLength = vecB - vecA; abLength = new Vector3($$anonymous$$athf.Abs(abLength.x), $$anonymous$$athf.Abs(abLength.y), $$anonymous$$athf.Abs(abLength.z)); // set middle-point position cube.transform.localPosition = midPointAB; // get the rotation of the line and set it Quaternion rotationAB = Quaternion.LookRotation(dirAB); cube.transform.rotation = rotationAB; // set the new length (scaling size from cube prefab is 1/80/1/) cube.transform.localScale = Vector3.forward * abLength.magnitude; // higher the checkpoints to y=60 cube.transform.localScale = new Vector3(cube.transform.localScale.x, cube.transform.localScale.y + 60f, cube.transform.localScale.z); // place it on terrain RaycastHit hit; if (Physics.Raycast(cube.transform.position, Vector3.down, out hit)) { cube.transform.position = hit.point; }
Your answer
![](https://koobas.hobune.stream/wayback/20220612152208im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using GetPixels to break down an image, and only getting the last part. 1 Answer
Applying explosion force to destructible object AFTER it's been destroyed 2 Answers
Instantiate an object on mouse/cursor position. 1 Answer
How to Find gameObject child. 3 Answers
Why is my dropdown not able to control my game countdown time? 1 Answer