Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float speed = 2;
- Vector3 target;
- void SetTarget(Vector3 pos) {
- target = pos;
- }
- void Update() {
- // get the vector from current position to target
- Vector3 targetDelta = target - transform.position;
- Vector3 targetDirection = targetDelta.normalized;
- // calculate direction and distance to move
- float moveStepSize = speed * Time.deltaTime;
- if (moveStepSize > targetDelta.magnitude) {
- // this step would overshoot target, so just set position
- transform.position = target;
- } else {
- // step towards direction
- transform.position += targetDirection * moveStepSize;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment