Untitled conversation
- User
using UnityEngine; using UnityEngine.UI; // Required for Image component public class DistanceVisualizer : MonoBehaviour { // SerializeField allows setting these in the Inspector [SerializeField] private Transform distanceBarImage; // Reference to our UI Image element [SerializeField] float X1; [SerializeField] float X2; [Header("Distance Bar Settings")] [Tooltip("The minimum width of the bar in UI pixels (when distance is 0 or very small).")] [SerializeField] private float minBarWidth = 50f; // Bar width when distance is near zero [Tooltip("The maximum width of the bar in UI pixels (when distance is at or beyond X2-X1).")] [SerializeField] private float maxBarWidth = 400f; // Max width of the bar on UI [SerializeField] GameObject winScreen; void Update() { // Ensure all references are set before trying to use them if ( distanceBarImage != null) { // Calculate the distance between the positions of X1 and X2 float currentDistance = transform.position.x-X1; // Normalize the distance to a 0-1 range // InverseLerp maps a value from a range [a, b] to a [0, 1] range. // Here, distance 0 maps to 0, and X2-X1 maps to 1. float normalizedDistance = Mathf.InverseLerp(0f, X2-X1, currentDistance); // Clamp the normalized distance to ensure it stays within 0 and 1 // This prevents the bar from going beyond maxBarWidth if distance > X2-X1 // or smaller than minBarWidth if distance is negative (though distance is always >=0). normalizedDistance = Mathf.Clamp01(normalizedDistance); // Interpolate the bar width based on the normalized distance // Lerp maps a [0, 1] value to a range [minBarWidth, maxBarWidth]. float targetWidth = Mathf.Lerp(minBarWidth, maxBarWidth, normalizedDistance); // Update the width of the UI Image's RectTransform distanceBarImage.position = new Vector3(targetWidth, distanceBarImage.position.y,distanceBarImage.position.z); } else { Debug.LogWarning("DistanceVisualizer references not set up! Please assign all in the Inspector."); } if(transform.position.x > X2) { winScreen.SetActive(true); } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.