format
typescript
Vector3.MoveTowards(source, target, max_delta_dis)class: Vector3
description
Moves a game object from its current position to a target position along a straight line.
Use this method to move the game object at the source position towards the target position. By updating the object's position with the calculated position from this method each frame, you can smoothly move towards the target.
parameter
| param_name | type | description |
|---|---|---|
| source | Vector3 | The current position. |
| target | Vector3 | The target position. |
| max_delta_dis | number | The maximum distance to move. |
return
| type | description |
|---|---|
Vector3 | The position after the movement. |
code example
typescript
class New_TypeScript
extends Component {
public speed:number;
private target:Vector3;
OnStart(): void {
Debug.Log("Start moving");
}
OnUpdate(): void {
// Current position
let source = this.transform.position;
// Target position
this.target = new Vector3(5,0,0);
// Maximum distance to move
let max_delta_dis = this.speed * Time.deltaTime;
// Position after each frame of movement
let position = Vector3.MoveTowards(source,this.target,max_delta_dis);
// Update the game object's position
this.transform.position = position;
}
}