Page5
GameObject: Transform and Trigger
Each GameObject has its Transform. It includes GameObject’sposition,rotation and scale. You can change them as you want. There are twoways of changing:
每个游戏对象都有它的Transform属性。它包含了游戏对象的位置,旋转和缩放。你可以任意改变它们。这里有两种方式:
1) You can bind them a new Vector3 with x,y and z values.
你可以给它们绑定一个带有x,y,z值的新三维空间向量
2) You can use special Transform’s methods:Translate() and Rotate();
你可以使用特殊的Teansform方法:Translate() and Rotate();
The first example:
第一个例子:
C# code:
public Vector3 pos; // Public Vector3 for the inspector检视面板的公有变量
void Start(){
gameObject.transform.position= pos; // Change position to the “pos”改变”pos”的位置
}
The second example:
第二个例子
C# code:
void Update(){
gameObject.transform.Translate(1*Time.deltaTime, 0, 0); // Add 1 tox every second
//每秒把x坐标加1
}
“Trigger” “触发器”
Trigger is an area of GameObject, which events you can trace (e.g.if someone enters the trigger, you will know about that).
触发器是游戏对象的一块区域,在此区域你可追踪事件(比如某人进入触发器,你就会知道这个事件)。
To create the GameObject’s trigger, add Collider (from the Componentmenu)and set a check mark in the “Is Trigger” box. You can edit trigger’s shape (there are six triggers in theComponent menu), size and its center.
为了创建游戏物体的触发器,可添加球型碰撞体(从组件菜单)并勾选“IsTrigger”框。你可以编辑触发器的形状(在组件菜单中有六种触发器),大小和它的中心。
Let’s create a portal! Create new C# script and write this:
让我们创建一个接口!创建新的C #脚本,代码如下:
C# code:
public GameObject portal2; // Thesecond portal. Assign it in the inspector
//第二个接口.可在检视面板中配置。
void OnTriggerEnter(Collider other){ // Ifsomeone enters the trigger如果某人进入触发器
other.gameObject.transform.position = portal2.transform.position;
}
Then:
然后:
1) Set Main Camera’s position to the (0, 1,-5).
设置主摄像机的位置为(0, 1, -5).
2) Create new Cube, call it “Portal1”, setits position to the (0, -1, 0) and its scale to the (1, 2, 1). Set “IsTrigger”box as true.
创建新的立方体,取名为“Pottal1”,设置它的位置为(0, -1, 0)缩放为(1, 2, 1)并设置“IsTrigger”box的值为真。
3) Create new Cube, call it “Portal2”, setits position to the (0, 3, 0) and its scale to the (1, 0.25,1).
创建新的立方体,取名为“Pottal2”,设置它的位置为(0, 3, 0)缩放为(1, 0.25, 1).
4) Create new Cube, call it “Cube”and setits position to the (0, 2, 0). Add Rigidbody Component from the Component menu.
创建新的立方体,取名为“Cube”,设置它的位置为(0, 2, 0).从组件菜单中添加刚体组件。