Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by Ninamori, Apr 19, 2016.

  1. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    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?
     
  2. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    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.
     
    lucidtripper likes this.
  3. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    Whoops I meant to say rotation. Transformation.rotation should work fine though. Thank you :).
     
  4. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    The only thing is I need the value of one the transforms (the y value) to be negative. How would I do that?
     
  5. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    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);
     
  6. Ninamori

    Ninamori

    Joined:
    Oct 30, 2015
    Posts:
    33
    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: http://pastebin.com/8PP3MMsH

    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 :(.
     
  7. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
  8. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    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.
    Code (csharp):
    1. CameraRig.transform.position = Player.transform.position;
    Then the player can match the camera's Y rotation like so.
    Code (csharp):
    1. Player.transform.eulerAngles = new Vector3(Player.transform.eulerAngles.x, -CameraHead.transform.eulerAngles.y, Player.transform.eulerAngles.z);