- Home /
Furthest point of object in center of another object
For my current Unity game, I need to set a point of the spawned object, in the center of an another object. I don't really know how to explain it, but I will try to do it with images, because it's a lot quicker and more clear.
When I spawn my object, the object's size is decided by the distance between two objects. It addapted it's size to the distance, as seen in this image.
But, as you can see, the center of the spawned object, is in the center of the round object, but the furthest point (The red circled point), needs to be in the center.
I use this code at the moment;
#pragma strict
static var iscreating = false;
var Routepoint: GameObject;
var Startpoint: GameObject;
var c1 : Color = Color.blue;
var c2 : Color = Color.blue;
var vehicle : Transform;
var routePathObject: Transform;
private var line : LineRenderer;
function Update() {
Routepoint = GameObject.FindWithTag ("Routepoint");
Startpoint = GameObject.FindWithTag ("Startpoint");
}
function OnGUI () {
if (iscreating == true) {
if (GUI.Button (Rect (240,40,120,20), "Confirm")){
var routePath : Transform = Instantiate(routePathObject, Startpoint.transform.position, transform.rotation);
var distance = Vector3.Distance(Startpoint.transform.position, Routepoint.transform.position);
routePath.LookAt(Routepoint.transform);
routePath.transform.localScale.z = distance;
routePath.transform.position = Startpoint.transform.position;
globalinfo.totalroutes += 1;
iscreating = false;
}
if (GUI.Button (Rect (240,70,120,20), "Cancel")){
iscreating = false;
}
}
}
I have no idea how to do this. I tried to transform the position after the size was set, but it didn't work. I also tried to set a different pivot point of the spawned object, but because the size is different everytime, it didn't work.
I would appreciate it if someone knows the answer.
I don't have time to provide a proper detailed answer, so I'll leave my thoughts as a comment, and perhaps they'll be of some help.
What you want is to find the position that is halfway between both objects- this can be done by simply subtracting the transform positions of the two objects, and dividing your answer by 2.
Now you need to know how many units your object takes up to begin with, as scale is relative to the original object. You already know the distance in units the object needs to cover from subtracting the positions, so now it's a matter of adjusting the scale so that the object fits in between.
for instance, if you were using a cube, those are 1 unit big. So you could just multiply 1 by the distance and get what you want. for objects that are not 1 unit to begin with, I think you'll need to subtract the units that they already cover from the distance.
Thanks for the answer. I get the first part of your answer. It's quite logical.
But I didn't get the last part of your answer, as for the units. What I can find out of your answer, I need to subtract the positions of both objects and multiply it with the 'units'?
When you drag the object into the scene normally, how big is it? If it's 2 units wide, you'll need to multiply the scale by the distance and divide by 2. if it's 5 units wide, divide by 5, etc.
This makes me wonder if there's a built in function to deter$$anonymous$$e an object's 100% scale size in units.
Thanks for the answer.
Where / how can you see the total Units of an object? It's not a built in function if I recall.
Your answer
Follow this Question
Related Questions
Locking the mouse cursor without it centering after unlocking it? 0 Answers
How to get position of multiple platforms? 0 Answers
Precise vertex positions 1 Answer
Camera rotation around player while following. 6 Answers
Find center between 4 points 3 Answers