Bind gameobject position to another

Instead of simply parenting of an object, I am trying to script a very unique situation but I am lost. I have 2 objects and a camera. The camera is already scripted to properly follow object 1. I would like object 2 to be bound to Object 1 by Global position only. IE, I would like it bound to the top of Object 1 but not rotate with Object 1. I would also like Object 2 to Rotate based on the rotation of the Camera but only around the Y axis.

I cannot seem to find information on how to make the specific Transform bind. I would think it is something like

Object2.Transform.Position = Object1.Transform.Position;

and

Object2.Transform.Rotation.Y = Camera.Transform.Rotation.Y;

But that is not working at all.

Any help is appreciated.

Try this for C# (not sure if it would work the same in JS)

public Transform Obj;
public Transform Cam;

void Update() {
    transform.position = Obj.position;
    transform.rotation = Quaternion.Euler(0, Cam.rotation.eulerAngles.y, 0);
}

You would attach the script to your “Object2”, then drag-drop the GameObjects from your scene into the script. You can not set rotation.y directly, also rotation is a Quaternion not Euler.

I was working with something similar today and finally got it working.

This may be redundant (e.g. I'm gonna lose another 5 karma) but make sure you have Object1 and Object2 defined as variables with a transform type e.g:

var Object1 : transform;
var Object2 : transform;

I found that I had to pass the Vector3 of the subject to a variable and then pass that variable to the function which performed the rotation on the target:

var targetposition : Object1.Transform.Position;
Object2.Transform.Position = targetposition

--line about fixedUpdate removed as it was misinformation, see comment below by Kleptomaniac--

If this manages to wig out or break when trying to work out rotation, it might be worth investigating Quaternion or eulerAngles. I would test more myself but I'm at the office.