- Home /
How to move one object to the exact position of another object?
I'm attempting to move one object to the exact position of another object, but I'm having weird issues with it. ObjectA is the object being moved; it has no parent. ObjectB has parents. I would think it would be as simple as:
ObjectA.transform.position = ObjectB.transform.position;
When I attempt this though, ObjectA is never on target. Any ideas on what I'm missing?
I think you need to provide a little more detail about the scene, since it actually is that simple under normal circumstances. Do the two objects use the same mesh? Are their origins where you think they are? Are they moving or colliding with other objects, or each other?
No collisions, different meshes, and I'm pretty darn sure their origins are in the proper place. Unless it was changed in the translation from the 3ds files via the fbx importer.
You could try to replace the mesh on ObjectA and ObjectB to something simple, like a sphere for the sake of testing
btw, are the objects far away after you set the position and rotation, or just slightly in the wrong position?
Answer by KvanteTore · Mar 25, 2010 at 09:55 PM
Setting transform.position
moves the pivot of the object (the local origin). Could it be that the pivots of ObjectA and ObjectB are not in the same place. Alternatively, could it be that the pivots of the objects are not at the center of the model, so that when ObjectA and ObjectB has different orientation it looks like they are not in the same position? Try setting the rotation as well
ObjectA.transform.rotation = ObjectB.transform.rotation;
Hey @$$anonymous$$vanteTore, you were totally right about that. The transforms are only relative in the inspector...my bad
Rotation before or after moving the object didn't help. I check the models in 3ds and I'm pretty sure the center is exactly where I want it to be.
@$$anonymous$$yle: Like others have mentioned, try doing it with two boxes (set the position of one box to be equal to the position of the other) and you will see that it works perfectly. The problem $$anonymous$$UST be something with your models, or other scripts interfering that you haven't mentioned.
Answer by e-bonneville · Mar 25, 2010 at 09:58 PM
EDIT: Complete answer redo!
ObjectB's transform is, indeed, relative to its parents. Its Vector3 coordinates are relative to its parent's location, so the reason your code isn't working correctly is because ObjectA is transforming to the correct location in worldspace, and its coordinates are relative to 0,0,0. However, ObjectB's coordinates are relative to the position of its parents, treating its parent's location as 0,0,0, which, understandbly causes problems, for if you set the transform of each object to 0,0,5, and ObjectB's parent is already at 0,0,5, ObjectB will treat its parent's location as 0,0,0, causing it to be offset by 10, instead of 5. This will cause its location to differ from ObjectA's. Use this code to translate relative space to worldspace:
var ObjectBPosition = function TransformDirection (0, 0, 0);
Put this code on ObjectB and save it as ObjectBCode.js. The next code goes on ObjectA.
var ObjectBCode : ObjectBCode;
function Update () {
transform.position = (ObjectB.ObjectBPosition.x, ObjectB.ObjectPosition.y, ObjectB.ObjectPosition.z);
}
Put this code on ObjectA and drag ObjectB (with ObjectBCode attached) onto the open slot in the Inspect. Press play.
P.S. ObjectC should be in the exact same spot as ObjectB, so if you put ObjectA in ObjectC's spot, it's the same spot as ObjectB.
transform.position
is the position in worldspace according to the documentation. I'm not sure what your snippet does. Care to elaborate?
Well, you use that to get the real position of ObjectB, so you can then apply the ObjectBSpot to the position of ObjectA, which should place it right on B. Wasn't a very good answer, didn't have much time. Sorry...
Answer by Tuti · Mar 25, 2010 at 11:16 PM
Look at this script bellow and attach it to an empty game object:
var object1 : GameObject;
var object2 : GameObject;
function Update () {
//Check if player hit left mouse button (default for Fire1) if (Input.GetButtonDown("Fire1")){
//Stores position and rotation of one of the objects var position1 : Vector3 = object1.transform.position; var rotation1 : Quaternion = object1.transform.rotation;
object1.transform.position = object2.transform.position; object1.transform.rotation = object2.transform.rotation;
object2.transform.position = position1; object2.transform.rotation = rotation1;
}
}
The example above is one of the correct ways to define a position based on another object's position.
Hope that helps.
The only difference between your code and the OP's code is that your code interchanges the positions between object1 and object2, whereas the OP's code only sets the position on one of the objects