Transfrom
1、移动
transform.position = Vector3.right * 2;
transform.position = new Vector3(2,0,0);
transform.Translate(new Vector3(2, 0, 0));
transform.Translate(Vector3.right * 2);
transform.Translate(2, 0, 0);
2、旋转
transform.rotation = Quaternion.Euler(0,45,0);
transform.Rotate(new Vector3(0,45,0));
3、缩放
transform.localScale = new Vector3(2f, 2f, 2f);
transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
transform.localScale = Vector3.one * 2;//乘2就是放大两倍
Vecto3
GameObject
GameObject和gameObject的区别
GameObject是游戏对象类的基类
gameObject是脚本挂载的对象
Time
Time.time:表示从游戏开始到现在的时间,会随着游戏的暂停而停止计算。
Time.deltaTime:表示从上一帧到当前帧的时间,以秒为单位。
Time.timeScale:时间缩放,默认值为1,若设置<1,表示时间减慢,若设置>1,表示时间加快,可以用来加速和减速游戏,非常有用。
克隆游戏对象
//在预设体的位置克隆游戏对象
GameObject go1=Instantiate(对象名);
//在固定位置克隆游戏对象
//Quaternion.identity 游戏对象不旋转:Quaternion(0,0,0)
GameObject go2 =Instantiate(cube,new Vector3(0,0,5),Quaternion.identity);
销毁游戏对象
Destroy(要销毁的对象名);
Destroy(go1); //直接销毁对象go1
Destroy(要销毁的对象名,几秒后销毁);
Destroy(go2,3); //停3秒后销毁对象go2
查找游戏对象
通过名称来查找游戏对象
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; //返回0.0(包括)到1.0(包括)之间的数。
int b=Random.Range(0,100) ; //包括最小但不包括最大
float c=Random.Range(0.0f,5.5f); //包括最大和最小
Input
1、鼠标事件
GetMouseButton(0):按下鼠标左键不动,程序会一直运行,松开左键程序停止运行。
GetMouseButton(2):按下鼠标中键不动,程序会一直运行,松开中键程序停止运行。
GetMouseButton(1):按下鼠标右键不动,程序会一直运行,松开右键程序停止运行。
GetMouseButtonDown(0):按下鼠标左键时,程序运行一次
GetMouseButtonDown(1):按下鼠标右键时,程序运行一次
GetMouseButtonUp(2):按下鼠标中键时,程序不运行,松开中键时,程序运行一次。
if(Input.GetMouseButton(0)){
执行语句;
}
2、键盘事件
GetKey 当通过名称指定的按键被用户按住时返回true
GetKeyDown 当用户按下指定名称的按键时的那一帧返回true。
GetKeyUp 在用户释放给定名字的按键的那一帧返回true。
GetAxis("Horizontal")和GetAxis("Vertical") 水平轴和垂直轴
if(Input.GetKey("Down")){
执行语句;
}