Unity 碰撞的例子

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

编辑完两个脚本后,关闭回到unity。

7.分别拖拽两个脚本"Player1"和"Player2"到Hierarchy的对应物体Player1和Player2上。

8.分别选择Hierarhy面板上的Player1和Player2物体,选择菜单 "Component" - "Physical" -- "RigidBody",为物体增加碰撞属性。

9.保存,运行。

"ADWSQE"  -- "左右前后上下"

"P"  -- 控制是Player1还是Player2接收按键的输入。

"HKUJYI"  --控制绕轴旋转的方向。

10.运行截图:

学习记录,方便复习
原文地址:https://www.cnblogs.com/jingjingdidunhe/p/6240924.html