本文主要是介绍unity小游戏-插针游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
public class Pin : MonoBehaviour
{public float speed = 15;//插针移动到发射点的速度public float speed1 = 25;//插针移动到目标位置点的速度private bool isFly = false;//是否往目标点发射private bool isReach = false;//是否到达就绪点private Transform startPoint;//起始点(空物体)private Transform circle;private Vector3 targetCirclePos;//Pin的目标位置点void Start(){startPoint = GameObject.Find("StartPoint").transform;//获取就绪点的Transformcircle = GameObject.Find("Circle").transform;//获取circle的TransformtargetCirclePos = circle.position;//圆形的位置点targetCirclePos.y -= 1.48f;//计算得到Pin的位置点}void Update(){if (isFly == false){if(isReach == false){transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed1 * Time.deltaTime);//往就绪点发射//如果插针和就绪点之间的距离小于0.05,表示插针到达位置点if (Vector3.Distance(transform.position, startPoint.position) < 0.05f){isReach = true;}}}else{transform.position = Vector3.MoveTowards(transform.position, targetCirclePos, speed * Time.deltaTime);//往目标点移动//如果Pin的位置和目标位置点的距离小于0.05if (Vector3.Distance(transform.position, targetCirclePos) < 0.05f){transform.position = targetCirclePos;transform.parent = circle;//把当前插针的父亲设置为circle 这样能够跟随父物体一起运动isFly = false;//不再运动}}}public void StartFly(){isFly = true;isReach = true;}
}
public class GameManager : MonoBehaviour
{private Transform startPoint;//就绪点private Transform spawnPoint;//开始点public GameObject pinPrefabs;private Pin currentPin;private bool isGameOver = false;private int score = 0;//定义分数变量public Text scoreText;//在面板直接拖拽赋值private Camera mainCamera;//游戏结束的渐变动画public float speed = 3;//设置动画渐变的速度// Start is called before the first frame updatevoid Start(){startPoint = GameObject.Find("StartPoint").transform;spawnPoint = GameObject.Find("SpawnPoint").transform;mainCamera = Camera.main;//获取主相机SpawnPin();}// Update is called once per framevoid Update(){if (isGameOver) return;if (Input.GetMouseButtonDown(0))//如果鼠标按键按下,分数+1{score++;scoreText.text=score.ToString();//.text的类型是StringcurrentPin.StartFly();SpawnPin();}}void SpawnPin(){currentPin = GameObject.Instantiate(pinPrefabs, spawnPoint.position, pinPrefabs.transform.rotation).GetComponent<Pin>();//根据pinPrefabs生成对象}//处理游戏的失败public void GameOver(){if (isGameOver) return;//如果isGameOver=true 直接游戏结束GameObject.Find("Circle").GetComponent<RotateSelf>().enabled = false;//脚本失效StartCoroutine(GameOverAnimatiion());//开启协程isGameOver = true;//游戏继续}IEnumerator GameOverAnimatiion(){while (true){mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed * Time.deltaTime);//插值,speed参数反映了变化的速度mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, 4, speed * Time.deltaTime);//lerp插值运算,朝目标颜色、size的变化if (Mathf.Abs(mainCamera.orthographicSize - 4) < 0.01){break;}yield return 0;}yield return new WaitForSeconds(1);//暂停一秒继续SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//重新加载场景}
}
public class RotateSelf : MonoBehaviour
{public float speed = 100;// Update is called once per framevoid Update(){transform.Rotate(new Vector3(0, 0, speed * Time.deltaTime));}
}
public class PinCircleTrigger : MonoBehaviour
{// Start is called before the first frame updateprivate void OnTriggerEnter2D(Collider2D other){//碰撞监测函数,如果主动碰撞的名字为PinHead,触发碰撞监测函数if (other.tag == "PinHead"){GameObject.Find("GameManager").GetComponent<GameManager>().GameOver();//GameManager对象的GameManager脚本的GameOver方法}}
}
这篇关于unity小游戏-插针游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!