飞行路径

Unity3d游戏开发

1. 建立一个名为FlyPath的飞行路径对象
2. 在FlyPath对象下,加载一个名为FlyCamera的相机
3. 建立一个空的Game Object,将FlyPath脚本拖放至该对象上

1 var path:GameObject;
2  var flyPathCamera:GameObject;
3 var mainCamera:GameObject;
4 var text2d:Texture2D[];
5 var flag : boolean;
6
7 function Start(){
8     path = GameObject.Find("FlyPath");
9     mainCamera = GameObject.Find("First Person Controller");
10     if( path!=null )
11     {
12         flyPathCamera = path.Find("FlyCamera");
13     }
14     flag=true;
15 }
16
17 function Update () {
18     if( path!=null && path.active )
19     {
20         var animation:Animation;
21         animation = path.GetComponent("Animation");
22         if( animation!=null )
23         {
24             if( !(animation.IsPlaying("Take 001")) )
25             {
26                 path.active = false;
27                 if( flyPathCamera!=null )
28                     flyPathCamera.active = false;
29                    
30                 if( mainCamera!=null )
31                     mainCamera.active = true;               
32             }
33             else if( mainCamera!=null && mainCamera.active )
34             {       
35                 mainCamera.active = false;
36             }
37         }
38     }
39 }
40
41 function  ActiveFlyPath(){
42     if( path==null || flyPathCamera==null || mainCamera==null )
43         return;
44    
45     bActive = !(path.active);
46     if( path.active!=bActive )
47     {
48         path.active = bActive;
49         flyPathCamera.active = bActive;
50         var animation:Animation;
51         animation = path.GetComponent("Animation");
52         if( animation!=null )
53         {
54             animation.active = bActive;
55             if( bActive && !animation.IsPlaying("Take 001") )
56                 animation.Play("Take 001");
57             else if( !bActive )
58                 animation.Stop();
59         }
60     }
61    
62     bActive = !bActive;
63     if( mainCamera!=bActive )
64     {
65         mainCamera.active = bActive;
66     }
67 }
68
69 function OnGUI ()
70 {
71     if(flag==true)
72     {
73         if(GUI.Button(Rect(0,0,80,20),text2d[0])){
74         ActiveFlyPath();
75         }
76         flag=false;
77     }
78     else {
79         if(GUI.Button(Rect(0,0,80,20),text2d[1])){
80         ActiveFlyPath();
81         }
82     }
83 }

你可能感兴趣的:(游戏,脚本)