一、Transform
1.移动
transform.position位置移动
transform.position = new Vector3(+10, 0, 0);
2.旋转
transform.rotation旋转角度
transform.rotation = Quaternion.Euler(0, 45, 0);
3.缩放
transform.localScale 自身缩放
transform.localScale=new Vector3(2,1,1);
二、Vector3
向量(Vector3)
既有大小又有方向的量叫做向量。在空间中,向量用一段有方向的线段来表示。
可用于描述具有大小和方向两个属性的物理量,例如物体运动的速度、加速度、摄像机观察方向、刚体受到的力等都是向量。
三、GameObject
GameObject和gameObject的区别
GameObject是游戏对象类的基类
gameObject是脚本挂载的对象
四、Time
Time.time:表示从游戏开始到现在的时间,会随着游戏的暂停而停止计算。
Time.deltaTime:表示从上一帧到当前帧的时间,以秒为单位。
Time.timeScale:时间缩放,默认值为1,若设置<1,表示时间减慢,若设置>1,表示时间加快,可以用来加速和减速游戏,非常有用。
五、克隆游戏对象
//在预设体的位置克隆游戏对象
GameObject go1=Instantiate(cube);
//在固定位置克隆游戏对象
//Quaternion.identity 游戏对象不旋转:Quaternion(0,0,0,0)
GameObject go2 =Instantiate(cube,new Vector3(0,0,5),Quaternion.identity);
六、销毁游戏对象
Destroy(go1);
Destroy(go2,3);
七、查找游戏对象
通过名称来查找游戏对象
GameObject cube1=GameObject.Find("Player");
GameObject cube2=GameObject.FindWithTag("Player");
八、添加获取组件
//1、把Move这个脚本加到cube这个游戏对象上
cube.AddComponent("Move");
//2、给游戏物体添加刚体
cube.AddComponent("Rigidbody");
//3、给游戏物体添加球体碰撞器
cube.AddComponent("BoxCollider");
//1、获取脚本组件Move
Move m=cube.GetComponent<Move>();
//2、获取刚体组件
Rigidbody r=cube.GetComponent<Rigidbody>();
九、Random
float a=Random.value;
int b=Random.Range(0,100) ;
float c=Random.Range(0.0f,5.5f);
十、Input
- 鼠标事件
GetMouseButton(0):按下鼠标左键不动,程序会一直运行,松开左键程序停止运行。
GetMouseButton(2):按下鼠标中键不动,程序会一直运行,松开中键程序停止运行。
GetMouseButton(1):按下鼠标右键不动,程序会一直运行,松开右键程序停止运行。
GetMouseButtonDown(0):按下鼠标左键时,程序运行一次
GetMouseButtonDown(1):按下鼠标右键时,程序运行一次
GetMouseButtonUp(2):按下鼠标中键时,程序不运行,松开中键时,程序运行一次。
if(Input.GetMouseButton(0)){
执行语句;
}