UML流程
靶子旋转 RotateSelf
public class RotateSelf : MonoBehaviour {
public float speed = 90;
// Update is called once per frame
void Update ()
{
transform.Rotate(new Vector3(0, 0, -speed * Time.deltaTime));
}
}
// Vector3(x,y,z)对应(红,绿,蓝)
//旋转修改z,像塑料小陀螺的小柄
针头 PinHead
//触发检测,调用游戏结束的方法
public class PinHead : MonoBehaviour {
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "PinHead")
{
GameObject.Find("GameManager").GetComponent<GameManager>().GameOver();
}
}
}
针 预制体 Pin
游戏控制器 GameManager
(了解) GetActiveScene
GetActiveScene().buildIndex);//得到当前激活的场景
Unity3D_(API)场景切换SceneManager
游戏管理者 GamaManager
代码
public class GameManager : MonoBehaviour {
[Tooltip("这里没用到,1号针的起飞位置")] private Transform startPoint;
[Tooltip("2号针的预备位置,也是生成位置")] private Transform spawnPoint;
[Tooltip("鼠标点击就起飞,1号针")] private Pin currentPin;
[Tooltip("数量、分数")] private int score = 0;
[Tooltip("靶子中间的文本框")] public Text scoreText;
[Tooltip("针预制体")] public GameObject pinPrefab;
//
[Tooltip("失败?")] private bool isGameOver = false;
[Tooltip("主相机")] private Camera mainCamera;
[Tooltip("游戏结束的动画的时长")] public float gameOverAnimationTime = 3;
[Tooltip("游戏结束,相机最终大小")] public float cameraOrthographicSizeWhenGameOver = 4f;
// Use this for initialization
void Start () {
startPoint = GameObject.Find("StartPoint").transform;//取
spawnPoint = GameObject.Find("SpawnPoint").transform;
mainCamera = Camera.main;
SpawnPin();//实例针
}
private void Update()
{
if (isGameOver) return;
if (Input.GetMouseButtonDown(0))
{
score++;//这逻辑是射出去就有分
scoreText.text = score.ToString();//UI更新
currentPin.StartFly();//1号起飞
SpawnPin();//2号预备
}
}
void SpawnPin()//生产针
{
Vector3 position = spawnPoint.position;
Quaternion quaternion = pinPrefab.transform.rotation;//头朝下
//GameObject.Instantiate(预制体,位置,旋转角度);
currentPin = Instantiate(pinPrefab, position, quaternion ).GetComponent<Pin>();
}
public void GameOver()//游戏结束
{
if (isGameOver) return;//暂时未发现不加的后果
//
GameObject.Find("Circle").GetComponent<RotateSelf>().enabled = false;//不转了。单例浪费,所以或者拖拽
StartCoroutine(GameOverAnimation()); //开协程,调用方法
isGameOver = true;
}
IEnumerator GameOverAnimation()//游戏结束的动画
{
while (true)
{
float size = cameraOrthographicSizeWhenGameOver;//嫌字多
//变色、变大
mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, gameOverAnimationTime * Time.deltaTime);
mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, size, gameOverAnimationTime * Time.deltaTime);
if( Mathf.Abs( mainCamera.orthographicSize- size )<0.01f)
{
break;
}
yield return 0;
}
yield return new WaitForSeconds(0.2f);//等待n秒后,加载新游戏
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//重新加载当前激活的场景
}
}
游戏结束
//旁边的Text是用来测试rtotateSelf的,因为觉得针跟着靶子转很神奇(结果是设置父节点的原因)
针 Pin
代码
public class Pin : MonoBehaviour {
[Tooltip("针的飞行速度")] public float speed = 5;
[Tooltip("起飞?")] private bool isFly = false;
[Tooltip("靶上?")] private bool isReach = false;
[Tooltip("1号针起飞位置")] private Transform startPoint;
[Tooltip("针扎上靶子时针的位置")] private Vector3 pinReachPos;
[Tooltip("靶子")] private Transform circle;
// Use this for initialization
void Start () {
startPoint = GameObject.Find("StartPoint").transform;
circle = GameObject.FindGameObjectWithTag("Circle").transform;
//circle = GameObject.Find("Circle").transform;
//
//
pinReachPos = circle.position;
pinReachPos.y -= 1.55f;
}
// Update is called once per frame
void Update () {
if (isFly == true)//1号针飞
{
transform.position = Vector3.MoveTowards(transform.position, pinReachPos, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, pinReachPos) < 0.05f)//差不多就得了,卡==0有时检测不到
{
transform.position = pinReachPos;
transform.parent = circle;//以此让针跟着靶子旋转
isFly = false;
}
}
else//2号针上位
{
if (isReach == false)
{
transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, startPoint.position) < 0.05f)//也是差不多得了
{
isReach = true;
}
}
}
}
public void StartFly()//鼠标左键调用GameManager,GameManager调用StartFly()
{
isFly = true;
isReach = true;
}
}