Getting position of an 3 D object ?
I am using Vuforia's Scanner app for scanning my object and OD file is added to unity, my problem is i want to gernerate a code which could tell me the postion of my 3 d object from diffrent camera position. I am new to coding and unity so forgive me for asking too naive thing.
Answer by IKDev · Aug 25, 2020 at 12:48 AM
You can do that by typing this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ExampleOne : MonoBehaviour
{
public GameObject yourObject;
void Update()
{
// Prints out position of your object every frame
Debug.Log(yourObject.transform.position);
}
}
Or, if you really want to have the position be displayed on a UI text object, here is code for normal Unity's text:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ExampleOne : MonoBehaviour
{
public GameObject yourObject;
public Text yourNormalText;
void Update()
{
// Sets the text of yourNormalText UI element to be yourObject's position (float). DON'T FORGET UnityEngine.UI NAMESPACE!!!
yourNormalText.text = yourObject.transform.position.ToString();
// Sets the text of yourNormalText UI element to be yourObject's position (int).
yourNormalText.text = yourObject.transform.position.ToString("0");
}
}
Here is a bit better code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ExampleOne : MonoBehaviour
{
public GameObject yourObject;
public Text yourNormalText;
void Update()
{
// Float
yourNormalText.text = "X: " + yourObject.transform.position.x.ToString() + " Y: " + yourObject.transform.position.y.ToString() + " Z: " + yourObject.transform.position.ToString();
// Int
yourNormalText.text = "X: " + yourObject.transform.position.x.ToString("0") + " Y: " + yourObject.transform.position.y.ToString("0") + " Z: " + yourObject.transform.position.z.ToString("0");
}
}
Code for TextMeshPro:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ExampleOne : MonoBehaviour
{
public GameObject yourObject;
public TextMeshProUGUI yourTextMeshProText;
void Update()
{
// Sets the text of yourNormalText UI element to be yourObject's position (float). DON'T FORGET TMPro NAMESPACE!!!
yourTextMeshProText.text = yourObject.transform.position.ToString();
// Sets the text of yourNormalText UI element to be yourObject's position (int).
yourTextMeshProText.text = yourObject.transform.position.ToString("0");
}
}
Bit better code for TextMeshPro:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ExampleOne : MonoBehaviour
{
public GameObject yourObject;
public TextMeshProUGUI yourTextMeshProText;
void Update()
{
// Float
yourTextMeshProText.text = "X: " + yourObject.transform.position.x.ToString() + " Y: " + yourObject.transform.position.y.ToString() + " Z: " + yourObject.transform.position.ToString();
// Int
yourTextMeshProText.text = "X: " + yourObject.transform.position.x.ToString("0") + " Y: " + yourObject.transform.position.y.ToString("0") + " Z: " + yourObject.transform.position.z.ToString("0");
}
}
Hope you find this helpful!
Answer by highpockets · Aug 24, 2020 at 10:12 PM
Ok, it sounds like you want to get the position of your object in space relative to a camera object, at least that is what I understand from your question.
[SerializeField] private Transform camTransform; //this is your other camera transform
[SerializeField] private Transform otherTransform; //this is your 3D object transform
private Vector3 posRelativeToCam;
private void Start()
{
posRelativeToCam = otherTransform.position - camTransform.position;
Debug.Log(“Position relative to Camera “ + posRelativeToCam);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612120633im_/https://answers.unity.com/themes/thub/images/avi.jpg)