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