Unity:API总结
1. 移动
1 public class Test : MonoBehaviour 2 { 3 public float speed; 4 5 void Update() 6 { 7 float hor = Input.GetAxis("Horizontal"); 8 float ver = Input.GetAxis("Vertical"); 9 transform.Translate(new Vector3(hor, 0, ver) * Time.deltaTime * speed); 10 11 //按世界坐标系移动 12 if (Input.GetKey(KeyCode.W)) 13 transform.position += Vector3.forward * Time.deltaTime * speed; 14 if (Input.GetKey(KeyCode.S)) 15 transform.position += Vector3.back * Time.deltaTime * speed; 16 if (Input.GetKey(KeyCode.A)) 17 transform.position += Vector3.left * Time.deltaTime * speed; 18 if (Input.GetKey(KeyCode.D)) 19 transform.position += Vector3.right * Time.deltaTime * speed; 20 21 22 if (Input.GetKey(KeyCode.W)) 23 transform.Translate(Vector3.forward * Time.deltaTime * speed, Space.World); 24 if (Input.GetKey(KeyCode.S)) 25 transform.Translate(Vector3.back * Time.deltaTime * speed, Space.World); 26 if (Input.GetKey(KeyCode.A)) 27 transform.Translate(Vector3.left * Time.deltaTime * speed, Space.World); 28 if (Input.GetKey(KeyCode.D)) 29 transform.Translate(Vector3.right * Time.deltaTime * speed, Space.World); 30 31 32 //按自身坐标系移动 33 if (Input.GetKey(KeyCode.W)) 34 transform.position += transform.forward * Time.deltaTime * speed; 35 if (Input.GetKey(KeyCode.S)) 36 transform.position += -transform.forward * Time.deltaTime * speed; 37 if (Input.GetKey(KeyCode.A)) 38 transform.position += -transform.right * Time.deltaTime * speed; 39 if (Input.GetKey(KeyCode.D)) 40 transform.position += transform.right * Time.deltaTime * speed; 41 42 //默认Space.Self,按自身坐标系移动 43 if (Input.GetKey(KeyCode.W)) 44 transform.Translate(Vector3.forward * Time.deltaTime * speed); 45 if (Input.GetKey(KeyCode.S)) 46 transform.Translate(Vector3.back * Time.deltaTime * speed); 47 if (Input.GetKey(KeyCode.A)) 48 transform.Translate(Vector3.left * Time.deltaTime * speed); 49 if (Input.GetKey(KeyCode.D)) 50 transform.Translate(Vector3.right * Time.deltaTime * speed); 51 } 52 }
1 public class Test : MonoBehaviour 2 { 3 public float force; 4 Rigidbody rd; 5 private void Start() 6 { 7 rd = GetComponent<Rigidbody>(); 8 } 9 10 void Update() 11 { 12 float hor = Input.GetAxis("Horizontal"); 13 float ver = Input.GetAxis("Vertical"); 14 Vector3 direction = new Vector3(hor, 0, ver); 15 rd.AddForce(direction * force); 16 rd.velocity = direction * force; 17 rd.MovePosition(transform.position + direction); 18 } 19 }
1 public class Test : MonoBehaviour 2 { 3 public float speed; 4 CharacterController cc; 5 private void Start() 6 { 7 cc = GetComponent<CharacterController>(); 8 } 9 void Update() 10 { 11 float hor = Input.GetAxis("Horizontal"); 12 float ver = Input.GetAxis("Vertical"); 13 cc.Move(new Vector3(hor, 0, ver) * Time.deltaTime * speed); //不受重力影响,返回值是枚举值表示碰撞信息 14 cc.SimpleMove(new Vector3(hor, 0, ver) * Time.deltaTime * speed); //受重力影响,返回值是布尔值表示角色是否着地 15 } 16 }
2.看向目标并向目标移动(插值运算)
1 public class Test : MonoBehaviour 2 { 3 public float speed; 4 private GameObject target; 5 private void Start() 6 { 7 target = GameObject.FindWithTag("Monster"); 8 } 9 void Update() 10 { 11 //1.看向目标 12 transform.LookAt(target.transform); 13 //2.向目标移动 14 //插值运算 返回值=to*t+(1-t)*from 会一直运算下去,要设置一个结束点 15 float dis = Vector3.Distance(transform.position, target.transform.position); 16 if (dis > 1) 17 transform.position = Vector3.Lerp(transform.position, target.transform.position, Time.deltaTime*speed); 18 } 19 }
3.旋转
欧拉角:有万向节锁问题,但可以旋转大于180度,x与z沿着自身坐标系旋转,y沿着世界坐标系旋转
四元数:没有万向节锁问题,但不能超过180度旋转
1 transform.eulerAngles = new Vector3(90, 0, 0); //欧拉角直接赋值 2 transform.rotation *= Quaternion.Euler(30,60,90); //四元数转欧拉角 3 transform.Rotate(transform.up, Time.deltaTime * speed); //绕自身y轴自转 4 transform.RotateAround(Vector3.zero, Vector3.up, Time.deltaTime * speed); //绕原点的y轴公转
3.1 看向目标
1 Vector3 dir = enemy.position - player.position; 2 dir.y = 0; //锁定y轴 3 player.rotation = Quaternion.LookRotation(dir); 4 5 player.LookAt(enemy); //没有锁定y轴,要避免人物向下看得保持y轴高度一致
3.2 用插值平滑的旋转
1 Vector3 dir = enemy.position - player.position; 2 dir.y = 0; 3 Quaternion target = Quaternion.LookRotation(dir); 4 player.rotation = Quaternion.Slerp(player.rotation, target, Time.deltaTime*speed);
3.3 按住鼠标右键调整视角
1 public class Test : MonoBehaviour 2 { 3 GameObject cam; 4 5 private void Start() 6 { 7 cam = GameObject.Find("Main Camera"); 8 } 9 void Update() 10 { 11 if (Input.GetMouseButton(1)) 12 { 13 float mouseX = Input.GetAxis("Mouse X"); //鼠标左右移动 14 float mouseY = Input.GetAxis("Mouse Y"); //鼠标上下移动 15 16 cam.transform.rotation *= Quaternion.Euler(new Vector3(0, mouseX, 0)); 17 cam.transform.rotation *= Quaternion.Euler(new Vector3(-mouseY, 0, 0)); 18 //或 19 cam.transform.Rotate(0,mouseX,0); 20 cam.transform.Rotate(-mouseY, 0, 0); 21 } 22 23 } 24 }
4.获取输入
public static float GetAxis(string axisName);
axisName列表可以在【Edit-ProjectSettings-Input】里查看
1 public class Test : MonoBehaviour 2 { 3 public float jumpSpeed; 4 public float moveSpeed; 5 public GameObject cam; 6 public float zoomSpeed; 7 void Update() 8 { 9 if (Input.GetKeyDown(KeyCode.Space)) //按下 10 GetComponent<Rigidbody>().AddForce(Vector3.up * jumpSpeed); 11 if (Input.GetKey(KeyCode.W)) //按住 12 transform.position += Vector3.forward * Time.deltaTime * moveSpeed; 13 if (Input.GetKeyUp(KeyCode.Escape)) //松开 14 Destroy(gameObject); 15 if (Input.GetMouseButton(0)) 16 Debug.Log("按住鼠标左键"); 17 if (Input.GetMouseButtonDown(1)) 18 Debug.Log("按下鼠标右键"); 19 if (Input.GetMouseButtonUp(2)) 20 Debug.Log("抬起鼠标中键"); 21 22 //鼠标中键放大缩小视野 23 float scroll = Input.GetAxis("Mouse ScrollWheel"); //return 0没有按下 0.1 往上滑 -0.1 往下滑 24 Camera ca = cam.GetComponent<Camera>(); 25 ca.fieldOfView += scroll * zoomSpeed; 26 } 27 }
5.时间类
1 void Update () { 2 Debug.Log("Time.time:" + Time.time); //游戏开始运行到现在执行的时间 3 Debug.Log("Time.deltaTime:" + Time.deltaTime); //当前帧所占用时间 4 Debug.Log("Time.timeScale:" + Time.timeScale);//控制时间流逝的速度。=1 游戏时间与实时时间匹配;=2游戏时间流速加倍;=0.5 游戏速度减半;=0 游戏时间完全“停止”。 5 } 6 private void FixedUpdate() 7 { 8 Debug.Log("Time.fixedTime:" + Time.fixedTime); 9 Debug.Log("Time.fixedDeltaTime:" + Time.fixedDeltaTime); //0.02 在Edit->ProjectSettings->Time的Fixed Timestep可以自行设置 10 }
6.定时器
1 //Time实现延时 2 public class Test1 : MonoBehaviour 3 { 4 float time; 5 6 void Update() 7 { 8 time += Time.deltaTime; //deltaTime代表当前帧时间 9 if (time > 3) 10 { 11 time = 0; 12 Debug.Log("3秒时间到!"); 13 } 14 } 15 } 16 17 //用协同程序实现延时 18 public class Test2 : MonoBehaviour 19 { 20 private void Start() 21 { 22 StartCoroutine(Delay()); 23 } 24 IEnumerator Delay() 25 { 26 yield return new WaitForSeconds(3); 27 Debug.Log("3秒时间到!"); 28 } 29 } 30 31 //Invoke实现延时 32 public class Test3 : MonoBehaviour 33 { 34 private void Start() 35 { 36 Invoke("Delay", 3); //延时3s再执行Delay()方法 37 InvokeRepeating("Delay", 1, 3); //第一次延时1s执行Delay()方法,后面每3s执行一次 38 } 39 void Delay() 40 { 41 Debug.Log("3秒时间到!"); 42 } 43 }
7.创建物体
1 //创建基本物体类型 2 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 3 //创建GameObject类型 4 GameObject go = new GameObject(); 5 //克隆 6 Instantiate(go);
8.激活物体
1 //激活状态 2 cube.SetActive(false); //设置激活状态,只能设置当前物体,不能设置父物体 3 Debug.Log(cube.activeInHierarchy); //父物体及子物体都激活才为true 4 Debug.Log(cube.activeSelf); //当前物体inspector中的checkbox是否被勾选
9.碰撞/触发检测
1 //碰撞函数 2 //条件:两个物体都有碰撞器,至少一方有刚体 3 private void OnCollisionEnter(Collision collision) 4 { 5 Debug.Log("两个物体刚刚碰撞上的一瞬"); 6 } 7 private void OnCollisionStay(Collision collision) 8 { 9 Debug.Log("两个物体正在碰撞"); 10 } 11 private void OnCollisionExit(Collision collision) 12 { 13 Debug.Log("两个物体碰撞后离开的一瞬"); 14 } 15 //触发函数 16 //条件:两个物体都有碰撞器,至少一方有刚体,至少一方IsTrigger要勾选 17 private void OnTriggerEnter(Collider other) 18 { 19 Debug.Log("两个物体刚刚接触的一瞬"); 20 } 21 private void OnTriggerStay(Collider other) 22 { 23 Debug.Log("两个物体正在互相接触"); 24 } 25 private void OnTriggerExit(Collider other) 26 { 27 Debug.Log("两个物体互相离开的一瞬"); 28 } 29 //角色控制器检测碰撞 30 //条件:一方必须有角色控制器,另一方挂载碰撞器 31 private void OnControllerColliderHit(ControllerColliderHit hit) 32 { 33 Debug.Log(hit.controller.name); //输出被碰撞物体的名称 34 } 35 //粒子碰撞/触发 36 //条件:一方有粒子系统,勾选Send Collision Messages属性,另一方有碰撞器 37 void OnParticleCollision(GameObject other) 38 { 39 Debug.Log("OnParticleCollison:"+other.name); 40 } 41 void OnParticleTrigger(GameObject other) 42 { 43 Debug.Log("OnParticleTrigger:" + other.name); 44 }
10.刚体
1 //用刚体移动 2 rd.MovePosition(transform.position + transform.forward * Time.deltaTime * speed); //瞬间移动用Position,匀速移动用MovePosition 3 //给刚体施加力 4 rd.AddForce(Vector3.forward * force , ForceMode.Impulse); 5 //模拟爆炸效果 6 public class Test : MonoBehaviour 7 { 8 public float radius = 5.0f; //爆炸半径 9 public float power = 10.0f; //爆炸力量 10 void Start() 11 { 12 Vector3 explosionPos = transform.position;//爆炸中心点 13 Collider[] colliders = Physics.OverlapSphere(explosionPos, radius); //返回球内与之接触的所有碰撞器 14 foreach (Collider hit in colliders) 15 { 16 Rigidbody rd = hit.GetComponent<Rigidbody>(); 17 if (rd != null) 18 rd.AddExplosionForce(power, explosionPos, radius, 3.0f); //第四个参数是下移爆炸中心的位置 19 } 20 } 21 }