Unity 3D- How to continually measure the distance between two calipers- one is stationary and the other can be moved using up and won arrow keys

I instantiate two cube objects to act as a caliper measuring device in a PI script

public void InstantiateCalipersAtDistance()
    {
        if (headCamera != null)
        {
            float heightInMeters = participantHeightCm / 100f;
            float distance = heightInMeters; // Distance in front of the participant
            Vector3 headPosition = headCamera.transform.position; // Get the head camera position
            Vector3 forward = transform.forward;

            Vector3 baseCaliperPosition = headPosition + forward * distance;
            caliperSeparationDistance = Random.Range(0.05f, 0.25f); // Random distance between 0.05 and 0.25 meters

            baseCaliper = Instantiate(baseCaliperPrefab, baseCaliperPosition, Quaternion.identity);
            movingCaliper = Instantiate(movingCaliperPrefab, baseCaliperPosition + new Vector3(0, caliperSeparationDistance, 0), Quaternion.identity);
        }
        else
        {
            Debug.LogError("Head camera is not assigned.");
        }
    }

The top caliper can be moved up and down but the base caliper stays put in a CC script

 void Update()
    {
        if (canMoveUp)
        {
            if (Input.GetKey(KeyCode.UpArrow))
            {
                transform.Translate(0, speed * Time.deltaTime, 0);

            }
        }
        if (canMoveDown)
        {
            if (Input.GetKey(KeyCode.DownArrow))
            {
                transform.Translate(0, -speed * Time.deltaTime, 0);

            }
        }
        
    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.name == "BaseCaliper(Clone)")
        {
            canMoveDown = false;


        }

    }
    void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.name == "BaseCaliper(Clone)")
        {
            canMoveDown = true;


        }

    }

I want to record the distance between the calipers continuously, even when the top one is moving. I tried to do this with a script I got from elsewhere, which I put on the moving (top) caliper object with the Obj field in the inspector having the bottom caliper assigned to it[enter image description here](https://i.sstatic.net/A211jBW8.png).

using UnityEngine;

public class LiveDistanceBetweenCalipers : MonoBehaviour
{
    
    [SerializeField] public GameObject obj; // Reference to the other caliper (base caliper)

    public float yDistanceBetweenObjects; // Variable to store the Y distance

    private void Update()
    {
        if (obj != null)
        {
            // Calculate the Y distance between this object (moving caliper) and the other object (base caliper)
            yDistanceBetweenObjects = Mathf.Abs(transform.position.y - obj.transform.position.y);

            // Print the Y distance to the console
            Debug.Log("Y Distance between calipers: " + yDistanceBetweenObjects);

            // Optionally, draw a line between the objects for visualization
            Debug.DrawLine(transform.position, obj.transform.position, Color.green);
        }
        else
        {
            Debug.LogWarning("Reference to the other caliper (base caliper) is missing.");
        }
    }

  
}

It seems to continually track a distance but I am pretty sure it is wrong as the line points to a different position in the scene form the top caliper, and the numbers I get don’t make sense relative to the distance.

Screenshots should hopefully make what I want to to more understandable if anyone can offer an assist.

New contributor

Mr Awesome is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật