Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class WayPointTraverser : MonoBehaviour {
  7.  
  8. public Transform[] waypoints;
  9. public Transform mainCamera;
  10.  
  11. public float speed = 3;
  12. public float startRotationDistance = 2;
  13. public float rotationSpeed = 5;
  14.  
  15. private Quaternion currentDesiredRotation;
  16.  
  17.  
  18.  
  19. void Start () {
  20. StartCoroutine(MoveThroughWaypoints());
  21. }
  22.  
  23. private IEnumerator MoveThroughWaypoints()
  24. {
  25. int currentWaypoint = 0;
  26. currentDesiredRotation = waypoints[currentWaypoint].rotation;
  27.  
  28. while (true)
  29. {
  30. mainCamera.position = Vector3.MoveTowards(mainCamera.position, waypoints[currentWaypoint].position, speed * Time.deltaTime);
  31. mainCamera.rotation = Quaternion.Lerp(mainCamera.rotation, currentDesiredRotation, rotationSpeed * Time.deltaTime);
  32.  
  33. if ((mainCamera.position - waypoints[currentWaypoint].position).magnitude <= startRotationDistance)
  34. {
  35. currentDesiredRotation = waypoints[currentWaypoint].rotation;
  36. }
  37.  
  38. if (mainCamera.position == waypoints[currentWaypoint].position)
  39. {
  40. currentWaypoint = (currentWaypoint + 1) % waypoints.Length;
  41. }
  42. yield return null;
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement