duck

duck

Nov 19th, 2010
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.66 KB | None | 0 0
  1.  
  2. float speed = 2;
  3. Vector3 target;
  4.  
  5. void SetTarget(Vector3 pos) {
  6.  
  7.     target = pos;
  8.  
  9. }
  10.  
  11. void Update() {
  12.  
  13.     // get the vector from current position to target
  14.     Vector3 targetDelta = target - transform.position;
  15.     Vector3 targetDirection = targetDelta.normalized;
  16.    
  17.     // calculate direction and distance to move
  18.     float moveStepSize = speed * Time.deltaTime;
  19.    
  20.     if (moveStepSize > targetDelta.magnitude) {
  21.         // this step would overshoot target, so just set position
  22.         transform.position = target;
  23.     } else {
  24.         // step towards direction
  25.         transform.position += targetDirection * moveStepSize;
  26.     }    
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment