一,Transform
Transform组件是Unity3D的重点之一,主要用于控制物体的旋转、移动、缩放。
API:application program interface 应用程序接口
位置移动 transform.position
transform.position = new Vector3(+10, 0, 0);
旋转角度transform.rotation
transform.rotation = Quaternion.Euler(0, 45, 0);
自生缩放transform.rotation
transform.localScale=new Vector3(2,1,1);
二,成员变量
向前移动
transform.Translate(Vector3.forwad);
transform.Translate(new Vector3(0,0,1));
transform.Translate(0,0,1);
旋转
游戏对象围绕Y轴旋转
transform.Rotate(new Vector3(0,1,0));
一个游戏对象围绕另一个游戏对象旋转
transform.RotateAround(sphere.position,Vector3.up,1)
transform.rotation和Rotate的区别:
Rotate()方法是:旋转多少度。在原有的基础上累加,即旋转了多少角度
rotation属性是:旋转到某个角度,就是是在update中每帧都执行。但每次旋转到的角度都是5,所以是旋转到5度。
比如你只想让他旋转到多少, 用rotation; 假如想让他一直转,可以用Rotate
rotation直接改变了数值, 以达到旋转效果
Rotate应用一个的旋转角度每秒1度慢慢的旋转物体
GameObject与gameObject的区别
GameObject是游戏对象类的基类
gameObject是脚本挂载的对象
三,Time
Time.time:表示从游戏开始到现在的时间,会随着游戏的暂停而停止计算。
Time.deltaTime:表示从上一帧到当前帧的时间,以秒为单位。
Time.timeScale:时间缩放,默认值为1,若设置<1,表示时间减慢,若设置>1,表示时间加快,可以用来加速和减速游戏,非常有用。
用deltaTime控制对象移动
public GameObject cube;
float speed = 3f;
void Update ()
{
cube.transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
倒计时
float timer = 6f;
public Text tap;
void Update () {
if (timer <= 1)
{
tap.text = "GO";
}
else
{
tap.text = ((int)timer).ToString();
}
timer -= Time.deltaTime;
}
四,克隆游戏对象
在预设体的位置克隆游戏对象
GameObject go1=Instantiate(cube);
在固定位置克隆游戏对象
Quaternion.identity 游戏对象不旋转:Quaternion(0,0,0,0)
GameObject go2 =Instantiate(cube,new Vector3(0,0,5),Quaternion.identity);
五,销毁游戏对象
Destroy(go1); //直接销毁对象go1
Destroy(go2,3); //停3秒后销毁对象go2