Mathf.MoveTowards Manual     Reference     Scripting  
Scripting > Runtime Classes > Mathf
Mathf.MoveTowards

static function MoveTowards (current : float, target : float, maxDelta : float) : float

Description

Moves a value current towards target.

This is essentially the same as Mathf.Lerp but instead the function will ensure that the speed never exceeds maxDelta. Negative values of maxDelta pushes the value away from target.

JavaScript
var target = 20.0;
var speed = 5.0;

function Update() {
transform.position = Vector3(Mathf.MoveTowards
(transform.position.x, target, speed * Time.deltaTime), 0, 0);
}

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public float target = 20.0F;
public float speed = 5.0F;
void Update() {
transform.position = new Vector3(Mathf.MoveTowards(transform.position.x, target, speed * Time.deltaTime), 0, 0);
}
}

import UnityEngine
import System.Collections

class example(MonoBehaviour):

public target as single = 20.0F

public speed as single = 5.0F

def Update():
transform.position = Vector3(Mathf.MoveTowards(transform.position.x, target, (speed * Time.deltaTime)), 0, 0)