How to access and modify/copy a transform of a object to another gameobject?

Basically what I want to do is access a game objects transform, copy it, and then update another game objects transform with the transform I just accessed. How do I do this?

In code? “transform” is the property you’re looking for. So for example:
otherObject.transform.position = myObject.transform.position; // Sets otherObject to the position of myObject

Repeat as necessary for any other aspects you want to copy.

1 Like

Whoops I meant to say rotation. Transformation.rotation should work fine though. Thank you :).

The only thing is I need the value of one the transforms (the y value) to be negative. How would I do that?

You would have to split it into a full Vector3.

otherObject.transform.position = new Vector3(myObject.transform.position.x, -myObject.transform.position.y, myObject.transform.position.z);

I think I’m asking the wrong question entirely. Basically what I’m trying to do is keep the HMD (working with a Vive here), player, and camera rig lined up. Since this is a seated experience is necessary. That way when the Vive HMD rotates so does the player.

So here is my code…

using UnityEngine;
using System.Collections;

public class HMDSync : MonoBehaviour
{

//This script is to keep rotations of player, camerarig, and camera (head) synced. Only thing is that the camerarig y value should be negative.

private GameObject CameraHead;
private GameObject CameraRig;
private GameObject Player;

void Start()
{
CameraHead = GameObject.Find(“Camera (eye)”);
CameraRig = GameObject.Find(“[CameraRig]”);
Player = GameObject.Find(“Player”);

if (CameraHead && CameraRig && Player != null)
{
Debug.Log(“All components for HMD Sync found!”);
}
else
{
Debug.Log(“Not all components for HMD Sync were found.”);
}
}

// Update is called once per frame
void Update()
{
CameraRig.transform.rotation = CameraHead.transform.rotation;
Player.transform.rotation = CameraHead.transform.rotation;
}
}

With proper formatting: using UnityEngine;using System.Collections;public class HMDSync : MonoBeha - Pastebin.com

Also here is the terrible result: https://vid.me/xFh1

I’m assuming to fix that mess I will have to only line up one or two axes (or whatever the plural of axis is). If not then I’m lost. I’m very confused :(.

http://forum.unity3d.com/threads/using-code-tags-properly.143875/

1 Like

I think I see the issue now from the video. The CameraRig/etc is parented to the Player, so when you try to match it, it will never reach, because Player moves all its children when it moves.

I would separate the CameraRig from the player, so its no longer nested, and just set its position to the Player.

CameraRig.transform.position = Player.transform.position;

Then the player can match the camera’s Y rotation like so.

Player.transform.eulerAngles = new Vector3(Player.transform.eulerAngles.x, -CameraHead.transform.eulerAngles.y, Player.transform.eulerAngles.z);