zeeawk

SimpleMove Problem

Oct 5th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. void Update()
  2. {  
  3.     CharacterController controller = transform.gameObject.GetComponent<CharacterController>();
  4.     // Note nodeX, nodeZ are int's not floats
  5.     float x = currentNode.nodeX;
  6.     float z = currentNode.nodeZ;
  7.                
  8.     // Direction of target
  9.     Vector3 direction = (new Vector3(x, 0, z)) - transform.position;
  10.     // Ignore Vertical
  11.     direction.y = 0;                                           
  12.  
  13.     float speed = 4.5f;
  14.     float distance = Vector3.Distance(new Vector3(x, 0, z), transform.position);
  15.  
  16.     // Do we actually need to move ?
  17.     if ( distance > 0.0 )
  18.     {
  19.         // Find target direction
  20.         Quaternion rotDir = Quaternion.LookRotation(direction);
  21.         // Gradually rotate towards target
  22.         float rotationSpeed = 5.0f;
  23.         transform.rotation = Quaternion.Slerp(transform.rotation, rotDir, rotationSpeed*Time.deltaTime);
  24.         controller.SimpleMove(transform.forward * speed);
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment