FlyCam.cs 1.2 KB

123456789101112131415161718192021222324252627
  1. using UnityEngine;
  2. using System.Collections;
  3. public class FlyCam : MonoBehaviour {
  4. private int currentWaypoint = 0;
  5. public float rotateSpeed = 1.0f;
  6. public float moveSpeed = 10.0f;
  7. public float magnitudeMax = 10.0f;
  8. void Update () {
  9. if (WaypointCam.waypoints.Length>0){
  10. Vector3 RelativeWaypointPosition = transform.InverseTransformPoint(new Vector3(WaypointCam.waypoints[currentWaypoint].position.x, WaypointCam.waypoints[currentWaypoint].position.y,WaypointCam.waypoints[currentWaypoint].position.z ) );
  11. Vector3 targetPoint =new Vector3(WaypointCam.waypoints[currentWaypoint].position.x,WaypointCam.waypoints[currentWaypoint].position.y,WaypointCam.waypoints[currentWaypoint].position.z );
  12. Quaternion targetrot = Quaternion.LookRotation ( targetPoint - transform.position);
  13. transform.rotation = Quaternion.Slerp(transform.rotation, targetrot, Time.deltaTime * rotateSpeed);
  14. var forward = transform.TransformDirection(Vector3.forward);
  15. transform.position += forward * moveSpeed*Time.deltaTime;
  16. if ( RelativeWaypointPosition.magnitude < magnitudeMax ) {
  17. currentWaypoint ++;
  18. if ( currentWaypoint >= WaypointCam.waypoints.Length ) {
  19. currentWaypoint = 0;
  20. }
  21. }
  22. }
  23. }
  24. }