- Home /
How to scale and move a cuboid so that it fit's between two points?
Hi, I'm trying to draw a cuboid of static height between where the user clicks the first time, and where the user clicks the second time. I could probably solve this problem using a bit of maths, but I'm hoping there's some clever way of doing it!
Cheers.
Answer by Bunny83 · Feb 22, 2011 at 05:22 PM
Well, the easiest way of doing that would be to parent your 1x1x1 standard cube in a empty GO but locally translated to Vector3(0, 0, 0.5). That way the center of the parent object is centered on one side of the cube. you just have to rotate the parents forward axis (aka z-axis) towards your target and set the parents z-scale to the distance between A and B.
// part of a C# script that should be on the parent object
void SetTarget(Vector3 target)
{
Vector3 direction = target - transform.position;
transform.rotation = Quaternion.LookRotation(direction);
transform.localScale = new Vector3(1,1,direction.magnitude);
}
(ps I wrote that from scratch without error-checking)
Anyway, I'm not a friend of scaling. I would try a procedural mesh approach. But that's up to you ;)
This code didn't quite work for me but it gave me a big push in the right direction. Thanks.
Answer by Jeston · Feb 22, 2011 at 05:42 PM
Here would be my attempt at trying this: Create a 1x1x1 unit cuboid object, either hidden or in a prefab. When the second click occurs, take the difference in position, transform the distance to the proper coordinate frame and then set the scale based on that distance. Since you have a unity cube made, the distance and scale should be proportional.
Answer by jpatinop80 · Oct 05, 2012 at 04:24 PM
I don't know if I understand what you said, but this code created a object "cube" setting two points for the scale, just set a cube object as a prefab.
#pragma strict
public var myCube : Transform;
public var Ancho : float = 3;
public var Alto : float = 10;
public var inputPosA : Vector3 = Vector3.zero;
public var inputPosB : Vector3 = Vector3.zero;
var result : Vector3;
//var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
function Update()
{
if (Input.GetMouseButtonDown(0)){
inputPosA = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0)){
inputPosB = Input.mousePosition;
DrawALine();
}
}
function DrawALine()
{
Vector3 posC = ((inputPosB - inputPosA) * 0.5F ) + inputPosA;
float lengthC = (inputPosB - inputPosA).magnitude; //C#
var sineC : float = ( inputPosB.y - inputPosA.y ) / lengthC;
var angleC : float = Mathf.Asin( sineC ) * Mathf.Rad2Deg;
if (inputPosB.x < inputPosA.x) {angleC = 0 - angleC;}
Debug.Log( "inputPosA" + inputPosA + " : inputPosB" + inputPosB + " : posC" + posC + " : lengthC " + lengthC + " : sineC " + sineC + " : angleC " + angleC );
var myLine : Transform = Instantiate( myCube, posC, Quaternion.identity );
myLine.localScale = Vector3(lengthC, Ancho, Alto);
myLine.rotation = Quaternion.Euler(0, 0, angleC);
}
Your answer
Follow this Question
Related Questions
Rotate a Cube by 90 degree 0 Answers
Cube Sorter 0 Answers
Instantiating Cube Prefabs with scale 3 Answers
3ds rotation problem 1 Answer
¿Is it possible to scale independently of the rotation? 0 Answers