Could someone explain me why is mathf.clamp not claming value of normalizedValue ? I tried using clamp in several ways before asking, but it jsut seems to be not affecting anything. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class HealthBar : MonoBehaviour { public float displayedMaxValue=100; public float displayedCurrentValue=100; private float normalizedValue; public Transform bar; // Start is called before the first frame update void Start() { displayedCurrentValue = -50; } private void Update() { Mathf.Clamp(normalizedValue = displayedCurrentValue / displayedMaxValue, 0, 1); bar.localScale = new Vector3(normalizedValue,1f,1f); } }
Because you are ignoring its result. Also, it's bad form to do an assignment within the argument to a function. Just don't do it. Perhaps you meant this? Code (csharp): normalizedValue = Mathf.Clamp(displayedCurrentValue / displayedMaxValue, 0, 1);