Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Update()
- {
- CharacterController controller = transform.gameObject.GetComponent<CharacterController>();
- // Note nodeX, nodeZ are int's not floats
- float x = currentNode.nodeX;
- float z = currentNode.nodeZ;
- // Direction of target
- Vector3 direction = (new Vector3(x, 0, z)) - transform.position;
- // Ignore Vertical
- direction.y = 0;
- float speed = 4.5f;
- float distance = Vector3.Distance(new Vector3(x, 0, z), transform.position);
- // Do we actually need to move ?
- if ( distance > 0.0 )
- {
- // Find target direction
- Quaternion rotDir = Quaternion.LookRotation(direction);
- // Gradually rotate towards target
- float rotationSpeed = 5.0f;
- transform.rotation = Quaternion.Slerp(transform.rotation, rotDir, rotationSpeed*Time.deltaTime);
- controller.SimpleMove(transform.forward * speed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment