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.
Mr Awesome is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.