Clamp01 does not work?


I am making a trigger for a door and I Lerp between start and target positions depending on the ratio value.
I want to clamp this ratio between 0 and 1 since the Vector3.Lerp method requires this.

using UnityEngine;
using System.Collections;

public class Door : MonoBehaviour {

	public bool activated;
	private Vector3 startPos;
	public Vector3 LocalDestination;
	private Vector3 WorldDestination;
	public float speed;
	public float ratio;

	void Start () {
		startPos = transform.position;
		WorldDestination = startPos + LocalDestination;

	void Update () {
			ratio += speed * Time.deltaTime;
		} else {
			ratio -= speed * Time.deltaTime;

		transform.position = Vector3.Lerp(startPos, WorldDestination, ratio);

The problem is that the varible ratio gets not clamped to 01, it constantly de/increases depending on the bool activated so I have to wait.

Thanks in advance.

float is a value type, so the way you are calling it, clamp is not changing the value of ratio, is just receiving ratio as an argument.

public static float Clamp01(float value);

try this:

ratio = Mathf.Clamp01(ratio);