环境:Unity5.4.0f3 Personal

1.新建一个3D的Unity工程。

2.菜单 "GameObject" - "3D Object",选择"Cube" 和 "Capsule"两个物体。在Hierarchy的面板中,拖拽Capsule到Cube的物体上。Cube为Capsule的子组件。

3.Hierarhy面板中,选择Cube,在Inspector面板的Transform选项卡,更改两个属性:Position和Scale。Cube重命名为"Visor"。

 

  • Set the Visor GameObject’s Scale to (0.95, 0.25, 0.5).
  • Set the Visor GameObject’s Position to: (0.0, 0.5, 0.24)

  在Project面板,右键Assets目录,"Create" - "Material" ,创建一个材质,命名为Black。选中,在Inspector面板的"Shader "- "Unit/Color"中,更改材质颜色为黑色。拖拽到Hierarchy面板的Cube对象上。

4.拖拽Hierarchy面板中的Capsule到Project面板的Assets目录下,生成一个预制件,重命名为Player,模仿玩家对象。删除Hierarchy面板的Cube。

5.拖拽Player预制件到Hierarchy面板中两次,分别命名为"Player1"和"Player2"。

6.在Assets目录中,新建一个scripts文件夹,右键"Create" - "C# Scripts ",新建两个脚本文件,分别命名为"Player1"和"Player2"。 两个脚本的代码如下:

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class Player1 : MonoBehaviour {
 5 
 6     public static bool selected = true;
 7     public int translateSpeed=1;
 8     public int rotateSpeed = 10;
 9 
10     // Use this for initialization
11     void Start () {
12         
13     }
14     
15     // Update is called once per frame
16     void Update () {
17         if (Input.GetKeyDown (KeyCode.P)) {
18             selected = !selected;
19         }
20     }
21 
22     void OnGUI(){
23         if (selected) {
24             // turn left
25             if (Input.GetKey (KeyCode.A)) {
26                 transform.Translate (Vector3.left * Time.deltaTime);
27             }
28 
29             // turn right
30             if (Input.GetKey (KeyCode.D)) {
31                 transform.Translate (Vector3.right * Time.deltaTime);
32             }
33 
34             // turn forward
35             if (Input.GetKey (KeyCode.W)) {
36                 transform.Translate (Vector3.forward * Time.deltaTime);
37             }
38 
39             // turn backward
40             if (Input.GetKey (KeyCode.S)) {
41                 transform.Translate (Vector3.back * Time.deltaTime);
42             }
43 
44             // turn up
45             if (Input.GetKey (KeyCode.Q)) {
46                 transform.Translate (Vector3.up * Time.deltaTime);
47             }
48 
49             // turn down
50             if (Input.GetKey (KeyCode.E)) {
51                 transform.Translate (Vector3.down * Time.deltaTime);
52             }                    
53 
54             // rotate x axis anticlockwise
55             if (Input.GetKey (KeyCode.H)) {
56                 transform.Rotate (Vector3.left * Time.deltaTime * rotateSpeed);
57             }
58 
59             // rotate x axis clockwise
60             if (Input.GetKey (KeyCode.K)) {
61                 transform.Rotate (Vector3.right * Time.deltaTime * rotateSpeed);
62             }
63 
64             // rotate z axis clockwise
65             if (Input.GetKey (KeyCode.U)) {
66                 transform.Rotate (Vector3.forward * Time.deltaTime * rotateSpeed);
67             }
68 
69             // rotate z axis anticlockwise
70             if (Input.GetKey (KeyCode.J)) {
71                 transform.Rotate (Vector3.back * Time.deltaTime * rotateSpeed);
72             }
73 
74             // rotate y axis clockwise
75             if (Input.GetKey (KeyCode.Y)) {
76                 transform.Rotate (Vector3.up * Time.deltaTime * rotateSpeed);
77             }
78 
79             // rotate y axis anticlokcwise
80             if (Input.GetKey (KeyCode.I)) {
81                 transform.Rotate (Vector3.down * Time.deltaTime * rotateSpeed);
82             }
83         }
84     }
85 }
Player1

相关文章: