跳一跳
1. 音频播放
PlayOneShot :
1 public class ExampleClass : MonoBehaviour 2 { 3 public AudioClip impact; 4 AudioSource audioSource; 5 6 void Start() 7 { 8 audioSource = GetComponent<AudioSource>(); 9 } 10 11 void OnCollisionEnter() 12 { 13 audioSource.PlayOneShot(impact, 0.7F); 14 } 15 }
还有 Play(), Stop() 函数。
2. 粒子特效
常用函数:Play(), Stop(), Pause()
3. 生成砖块(随机方向。随机距离,随即大小,随机颜色)
1 // 添加砖块 2 public void CreateBrick() 3 { 4 int randomDir = Random.Range(0, 2); 5 brickDir = ((randomDir == 0) ? new Vector3(1, 0, 0) : new Vector3(0, 0, 1)); // 随机生成方向 6 float distance = Random.Range(5.0f, 10.0f); // 随机距离 7 float scale = Random.Range(2.0f, 5.0f); // 随机大小 8 Color color = new Color(Random.Range(0f, 1), Random.Range(0f, 1), Random.Range(0f, 1)); // 随机颜色 9 // 生成砖块 10 GameObject itemGo = GameObject.Instantiate(brickPreafab, currentBrick.transform.position + brickDir * distance, Quaternion.identity); 11 itemGo.transform.localScale = new Vector3(scale, 2f, scale); 12 itemGo.GetComponent<MeshRenderer>().material.color = color; 13 lastBrick = itemGo; 14 }
4. 刚体施加力
AddForce 函数。
1 public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force);
ForceMode :
5. 相机跟随(插值)
1 void LateUpdate() 2 { 3 MoveCamera(); // 移动相机 4 } 5 6 // 移动相机 7 private void MoveCamera() 8 { 9 if (isMoveCamera) // 相机跟随玩家移动效果 10 { 11 Vector3 targetPos = relativeOffestWithCamera + this.transform.position; // 相机当前位置 12 Vector3 startPos = Camera.main.transform.position; 13 Camera.main.transform.position = Vector3.Lerp(startPos, targetPos, 0.3f); // 插值 14 if (Vector3.Distance(targetPos, Camera.main.transform.position) < 0.1f) // 到达目标位置 15 { 16 isMoveCamera = false; 17 } 18 } 19 }