![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using UnityEngine;using System.Collections;using System.Collections.Generic;using UnityEngine.UI;using UnityEditor;////// 游戏控制/// public class bowAndArrow : MonoBehaviour { ////// Ray /// private Ray mouseRay1; ////// RaycastHit /// private RaycastHit rayHit; ////// 射线击中的x轴位置 /// private float posX; ////// 射线击中的y轴位置 /// private float posY; ////// 弓绳 /// public GameObject bowString; ////// 箭 /// GameObject arrow; ////// 箭预设 /// public GameObject arrowPrefab; ////// 用来作为射出的箭的父物体 /// public GameObject gameManager; ////// 伤害文本 /// public GameObject risingText; ////// 目标 /// public GameObject target; ////// 拉弓的声音 /// public AudioClip stringPull; ////// 放箭的声音 /// public AudioClip stringRelease; ////// 箭飞行的声音 /// public AudioClip arrowSwoosh; ////// 拉弓的声音是否已播放 /// bool stringPullSoundPlayed; ////// 放箭的声音是否已播放 /// bool stringReleaseSoundPlayed; ////// 箭飞行的声音是否已播放 /// bool arrowSwooshSoundPlayed; ////// 弓的位置 /// private ListbowStringPosition; /// /// 弓的线条渲染器 /// LineRenderer bowStringLinerenderer; float arrowStartX; float length; ////// 箭是否已射出 /// bool arrowShot; ////// 箭是否准备好 /// bool arrowPrepared; // position of the line renderers middle part Vector3 stringPullout; Vector3 stringRestPosition = new Vector3 (-0.44f, -0.06f, 2f); ////// 游戏状态 /// public enum GameStates { ////// 目录 /// menu, ////// 教程 /// instructions, ////// 游戏中 /// game, ////// 游戏结束 /// over, ////// 最高分 /// hiscore, }; ////// 游戏状态 /// public GameStates gameState = GameStates.menu; ////// 目录面板 /// public Canvas menuCanvas; ////// 指示面板 /// public Canvas instructionsCanvas; ////// 最高分面板 /// public Canvas highscoreCanvas; ////// 游戏面板 /// public Canvas gameCanvas; ////// 游戏结束面板 /// public Canvas gameOverCanvas; ////// 箭的数量文本 /// public Text arrowText; ////// 得分文本 /// public Text scoreText; ////// 最后得分文本 /// public Text endscoreText; ////// 最高得分 /// public Text actualHighscoreText; ////// 新的最高得分 /// public Text newHighscoreText; ////// 新的最高得分提示文本 /// public Text newHighText; ////// 每局能使用的箭数量 /// public int arrows = 20; ////// 实际得分 /// public int score = 0; ////// 重置游戏 /// void resetGame() { arrows = 20; score = 0; //如果没有找到arrow,创建新的arrow if (GameObject.Find("arrow") == null) createArrow (true); } void Start () { //设置ui显隐 menuCanvas.enabled = true; instructionsCanvas.enabled = false; highscoreCanvas.enabled = false; gameCanvas.enabled = false; gameOverCanvas.enabled = false; initScore (); createArrow (true); bowStringLinerenderer = bowString.AddComponent(); bowStringLinerenderer.SetVertexCount(3); bowStringLinerenderer.SetWidth(0.05F, 0.05F); bowStringLinerenderer.useWorldSpace = false; bowStringLinerenderer.material = Resources.Load ("Materials/bowStringMaterial") as Material; bowStringPosition = new List (); bowStringPosition.Add(new Vector3 (-0.44f, 1.43f, 2f)); bowStringPosition.Add(new Vector3 (-0.44f, -0.06f, 2f)); bowStringPosition.Add(new Vector3 (-0.43f, -1.32f, 2f)); bowStringLinerenderer.SetPosition (0, bowStringPosition [0]); bowStringLinerenderer.SetPosition (1, bowStringPosition [1]); bowStringLinerenderer.SetPosition (2, bowStringPosition [2]); arrowStartX = 0.7f; stringPullout = stringRestPosition; } void Update () { //检查游戏状态 switch (gameState) { //目录状态,按下esc 退出游戏 case GameStates.menu: if (Input.GetKeyDown(KeyCode.Escape)) {#if UNITY_EDITOR EditorApplication.isPlaying = false;#else Application.Quit();#endif } break; case GameStates.game: showArrows(); showScore(); //按下esc 显示目录 if (Input.GetKeyDown(KeyCode.Escape)) { showMenu(); } //如果按下鼠标左键 if (Input.GetMouseButton(0)) { //播放拉弓的声音 if (!stringPullSoundPlayed) { GetComponent ().PlayOneShot(stringPull); stringPullSoundPlayed = true; } prepareArrow(); } //如果鼠标抬起且箭已准备就绪 if (Input.GetMouseButtonUp (0) && arrowPrepared) { //播放放箭的声音 if (!stringReleaseSoundPlayed) { GetComponent ().PlayOneShot(stringRelease); stringReleaseSoundPlayed = true; } //播放箭飞行的声音 if (!arrowSwooshSoundPlayed) { GetComponent ().PlayOneShot(arrowSwoosh); arrowSwooshSoundPlayed = true; } shootArrow(); } drawBowString(); break; case GameStates.instructions: break; case GameStates.over: break; case GameStates.hiscore: break; } } /// /// 初始化得分 /// public void initScore() { if (!PlayerPrefs.HasKey ("Score")) PlayerPrefs.SetInt ("Score", 0); } ////// 显示得分 /// public void showScore() { scoreText.text = "Score: " + score.ToString(); } ////// 显示箭数量 /// public void showArrows() { arrowText.text = "Arrows: " + arrows.ToString (); } ////// 创建箭 /// /// 是否击中目标 public void createArrow(bool hitTarget) { Camera.main.GetComponent().resetCamera (); stringPullSoundPlayed = false; stringReleaseSoundPlayed = false; arrowSwooshSoundPlayed = false; //如果还有可使用的箭 if (arrows > 0) { //如果击中目标,随机设置目标的位置 if (hitTarget) { float x = Random.Range(-1f,8f); float y = Random.Range(-3f,3f); Vector3 position = target.transform.position; position.x = x; position.y = y; target.transform.position = position; } //创建一个新箭 this.transform.localRotation = Quaternion.identity; arrow = Instantiate (arrowPrefab, Vector3.zero, Quaternion.identity) as GameObject; arrow.name = "arrow"; arrow.transform.localScale = this.transform.localScale; arrow.transform.localPosition = this.transform.position + new Vector3 (0.7f, 0, 0); arrow.transform.localRotation = this.transform.localRotation; arrow.transform.parent = this.transform; arrow.GetComponent ().setBow (gameObject); arrowShot = false; arrowPrepared = false; arrows --; } else { gameState = GameStates.over; gameOverCanvas.enabled = true; endscoreText.text = "You shot all the arrows and scored " + score + " points."; } } /// /// 射箭 /// public void shootArrow() { if (arrow.GetComponent() == null) { arrowShot = true; arrow.AddComponent (); arrow.transform.parent = gameManager.transform; arrow.GetComponent ().AddForce (Quaternion.Euler (new Vector3(transform.rotation.eulerAngles.x,transform.rotation.eulerAngles.y,transform.rotation.eulerAngles.z))*new Vector3(25f*length,0,0), ForceMode.VelocityChange); } arrowPrepared = false; stringPullout = stringRestPosition; Camera.main.GetComponent ().resetCamera (); Camera.main.GetComponent ().setArrow (arrow); } /// /// 准备弓箭 /// public void prepareArrow() { mouseRay1 = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(mouseRay1, out rayHit, 1000f) && arrowShot == false) { posX = this.rayHit.point.x; posY = this.rayHit.point.y; Vector2 mousePos = new Vector2(transform.position.x-posX,transform.position.y-posY); float angleZ = Mathf.Atan2(mousePos.y,mousePos.x)*Mathf.Rad2Deg; transform.eulerAngles = new Vector3(0,0,angleZ); length = mousePos.magnitude / 3f; length = Mathf.Clamp(length,0,1); stringPullout = new Vector3(-(0.44f+length), -0.06f, 2f); Vector3 arrowPosition = arrow.transform.localPosition; arrowPosition.x = (arrowStartX - length); arrow.transform.localPosition = arrowPosition; } arrowPrepared = true; } ////// 画弓绳 /// public void drawBowString() { bowStringLinerenderer = bowString.GetComponent(); bowStringLinerenderer.SetPosition (0, bowStringPosition [0]); bowStringLinerenderer.SetPosition (1, stringPullout); bowStringLinerenderer.SetPosition (2, bowStringPosition [2]); } /// /// 设置得分 /// /// 分数 public void setPoints(int points){ score += points; //如果得分是50 if (points == 50) { //加一可用弓数量 arrows++; //创建提示文本 GameObject rt1 = (GameObject)Instantiate(risingText, new Vector3(0,0,0),Quaternion.identity); rt1.transform.position = this.transform.position + new Vector3(0,0,0); rt1.transform.name = "rt1"; rt1.GetComponent().text= "Bonus arrow"; } } /// /// 隐藏指引面板 /// public void showInstructions() { menuCanvas.enabled = false; instructionsCanvas.enabled = true; } ////// 显示指引面板 /// public void hideInstructions() { menuCanvas.enabled = true; instructionsCanvas.enabled = false; } ////// 显示最高得分 /// public void showHighscore() { menuCanvas.enabled = false; highscoreCanvas.enabled = true; actualHighscoreText.text = "Actual Hiscore: " + PlayerPrefs.GetInt ("Score") + " points"; newHighscoreText.text = "Your Score: " + score + " points"; if (score > PlayerPrefs.GetInt("Score")) newHighText.enabled = true; else newHighText.enabled = false; } ////// 隐藏最高得分 /// public void hideHighScore() { menuCanvas.enabled = true; highscoreCanvas.enabled = false; if (score > PlayerPrefs.GetInt ("Score")) { PlayerPrefs.SetInt("Score",score); } resetGame(); } ////// 检查最高分 /// public void checkHighScore() { gameOverCanvas.enabled = false; if (score > PlayerPrefs.GetInt ("Score")) { showHighscore(); } else { menuCanvas.enabled = true; resetGame(); } } ////// 开始游戏 /// public void startGame() { menuCanvas.enabled = false; highscoreCanvas.enabled = false; instructionsCanvas.enabled = false; gameCanvas.enabled = true; gameState = GameStates.game; } ////// 显示目录 /// public void showMenu() { menuCanvas.enabled = true; gameState = GameStates.menu; resetGame (); }}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using UnityEngine;using System.Collections;////// 控制弓箭/// public class rotateArrow : MonoBehaviour { ////// 是否已经 /// bool collisionOccurred; ////// 箭的头部 /// public GameObject arrowHead; ////// 伤害文本 /// public GameObject risingText; ////// 弓 /// public GameObject bow; ////// 击中目标的声音 /// public AudioClip targetHit; ////// 透明度 /// float alpha; ////// 剩余时间 /// float life_loss; ////// 颜色 /// public Color color = Color.white; void Start () { float duration = 2f; life_loss = 1f / duration; alpha = 1f; } void Update () { //刚体不为空 if (transform.GetComponent() != null) { //速度不为0 if (GetComponent ().velocity != Vector3.zero) { //获取速度 Vector3 vel = GetComponent ().velocity; //计算旋转角度 float angleZ = Mathf.Atan2(vel.y,vel.x)*Mathf.Rad2Deg; float angleY = Mathf.Atan2(vel.z,vel.x)*Mathf.Rad2Deg; //沿着弓箭的轨道旋转箭 transform.eulerAngles = new Vector3(0,-angleY,angleZ); } } //如果发生了碰撞 if (collisionOccurred) { //改变透明度和颜色 alpha -= Time.deltaTime * life_loss; GetComponent ().material.color = new Color(color.r,color.g,color.b,alpha); //如果透明度为0,删除物体 if (alpha <= 0f) { //创建新的箭 bow.GetComponent ().createArrow(true); Destroy(gameObject); } } } void OnCollisionEnter(Collision other) { //击中的位置 y轴 float y; //得分 int actScore = 0; //如果发生了碰撞 if (collisionOccurred) { //固定箭的位置 transform.position = new Vector3(other.transform.position.x,transform.position.y,transform.position.z); return; } ///如果碰到边界,销毁箭 if (other.transform.name == "Cube") { bow.GetComponent ().createArrow(false); Destroy(gameObject); } //如果碰到了目标 if (other.transform.name == "target") { //播放声音 GetComponent ().PlayOneShot(targetHit); //让速度归零 GetComponent ().velocity = Vector3.zero; //让刚体不受重力影响 GetComponent ().isKinematic = true; //让刚体不能发生旋转和位移 transform.GetComponent ().constraints = RigidbodyConstraints.FreezeAll; //设置标志位为true collisionOccurred = true; //隐藏箭头 arrowHead.SetActive(false); //修改y轴位置 y = other.contacts[0].point.y; y = y - other.transform.position.y; //射中白圈 if (y < 1.48557f && y > -1.48691f) actScore = 10; //射中黑圈 if (y < 1.36906f && y > -1.45483f) actScore = 20; //射中篮圈 if (y < 0.9470826f && y > -1.021649f) actScore = 30; //射中红圈 if (y < 0.6095f && y > -0.760f) actScore = 40; //射中金圈 if (y < 0.34f && y > -0.53f) actScore = 50; //创建得分文本 GameObject rt = (GameObject)Instantiate(risingText, new Vector3(0,0,0),Quaternion.identity); rt.transform.position = other.transform.position + new Vector3(-1,1,0); rt.transform.name = "rt"; rt.GetComponent ().text= "+"+actScore; bow.GetComponent ().setPoints(actScore); } } /// /// 设置弓 /// /// public void setBow(GameObject _bow) { bow = _bow; }}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using UnityEngine;using System.Collections;////// 相机移动/// public class camMovement : MonoBehaviour { ////// 弓箭 /// public GameObject arrow; void Start () { arrow = null; } public void setArrow(GameObject _arrow) { arrow = _arrow; } ////// 重置相机位置 /// public void resetCamera() { transform.position = new Vector3 (0, 0, -9.32f); } void Update () { //让摄像机跟随箭头移动 if (arrow != null) { Vector3 position = transform.position; float z = position.z; position = Vector3.Lerp (transform.position, arrow.transform.position, Time.deltaTime); position.z = z; transform.position = position; } }}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using UnityEngine;using System.Collections;////// 云层移动/// public class cloudMove : MonoBehaviour { ////// 移动速度 /// public float speed; void Update () { //获得当前位置 Vector3 position = transform.position; //设置新的x值 position.x += speed; //如果超出边界,重新移动到起始位置 if (position.x > 12f) position.x = -12f; //设置新的位置 transform.position = position; }}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using UnityEngine;using System.Collections;////// 向上飘的文字/// [RequireComponent(typeof(TextMesh))]public class RisingText : MonoBehaviour{ ////// 移动的增量 /// Vector3 crds_delta; ////// 透明度 /// float alpha; ////// 每秒减少的透明度 /// float life_loss; ////// 相机 /// Camera cam; ////// 颜色 /// public Color color = Color.white; void Start() { //初始透明度 alpha = 1f; cam = GameObject.Find("Main Camera").GetComponent(); //增量设置为向上 crds_delta = new Vector3(0f, 1f, 0f); life_loss = 0.5f; } void Update () { //向上移动 transform.Translate(crds_delta * Time.deltaTime, Space.World); //修改颜色 alpha -= Time.deltaTime * life_loss; GetComponent ().material.color = new Color(color.r,color.g,color.b,alpha); //如果完全透明,删除物体 if (alpha <= 0f) Destroy(gameObject); //让物体朝向相机 transform.LookAt(cam.transform.position); transform.rotation = cam.transform.rotation; }}
视频:https://pan.baidu.com/s/1nva2xWt
项目:https://pan.baidu.com/s/1nvc4t9R