SIKI学习1——见缝插针12脚本总结

1.GameManager脚本挂载在GameManager空物体上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;//加载场景命名空间引用
public class GameManager : MonoBehaviour
{
    private Transform StartPoint;//针的发射点
    private Transform SpawnPoint;//针的生成点
    public GameObject pinPrefab;//针的预设物,需要拖拽
    private Pin currentPin;//获取当前针,以方便调用方法
    private bool isGameOver = false;//判定游戏是否结束
    private int score = 0;
    public Text scoreText;//控制分数的显示
    private Camera mainCamera;
    public float speed = 3;//动画的速度
    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();
            currentPin.StartFly();//调用飞行的方法
            SpawnPin();
        }
    }
    void SpawnPin()
    {
        currentPin= GameObject.Instantiate(pinPrefab,SpawnPoint.position,pinPrefab.transform.rotation).GetComponent();
    }
    public void GameOver()
    {
        if (isGameOver) return;//保证这个方法只执行一次
        GameObject.Find("Circle").GetComponent().enabled = false;
        StartCoroutine(GameOverAnimation());
        isGameOver = true;
    }
    //利用协程控制动画
    IEnumerator GameOverAnimation()
    {
        while (true)
        {
            mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed * Time.deltaTime);//差值进行改变
            mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, 4, speed * Time.deltaTime);
            if (Mathf.Abs(mainCamera.orthographicSize-4)<0.01f)
            {
                break;                                                                                                                                                                                                                                                                                                                    
            }
            yield return 0;
        }
        yield return new WaitForSeconds(0.2f);//等待0.2秒重新加载
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//游戏结束后,重新加载当前场景
    }
}

2.Pin脚本挂载在Pin预设物上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pin : MonoBehaviour
{
    public float speed = 15;//移动速度
    private bool isFly = false;//表示是否发射针到小球中
    private bool isReach = false;//表示针是否到达指定的发射位置
    private Transform StartPoint;//小球的就位位置
    private Transform circle;//获取小球
    private Vector3 targetCirclePos;//针插入小球表面的差值
    void Start ()
    {
        StartPoint = GameObject.Find("StartPoint").transform;
        //circle = GameObject.Find("Circle").transform;
        circle = GameObject.FindGameObjectWithTag("Circle").transform;//通过标签查找小球跟上面的等价
        targetCirclePos = circle.position;
        targetCirclePos.y -= 1.541f;
    }
    void Update ()
    {
        if (isFly == false)
        {
            if (isReach == false)
            {
                transform.position = Vector3.MoveTowards(transform.position, StartPoint.position, speed * Time.deltaTime);//控制针移动到startPoint
                if (Vector3.Distance(transform.position, StartPoint.position) < 0.05f)//判断是否到达
                {
                    isReach = true;
                }
            }
        }
        else
        {
            transform.position = Vector3.MoveTowards(transform.position, targetCirclePos, speed * Time.deltaTime);//控制针移动到startPoint
            if (Vector3.Distance(transform.position, targetCirclePos) < 0.05f)
            {
                transform.position = targetCirclePos;
                transform.parent = circle;
                isFly = false;
            }
        }
    }
    public void StartFly()//控制针的飞行
    {
        isFly = true;
        isReach = true;
    }
}

3.PinHead脚本挂载在Pin预设物下的PinHead上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PinHead : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "PinHead")
        {
            GameObject.Find("GameManager").GetComponent().GameOver();
        }
    }
}

4.RotateSelf脚本挂载在小球上用于旋转自身

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateSelf : MonoBehaviour
{
    public float speed = 90;//旋转的度数
	void Update ()
    {
        transform.Rotate(new Vector3(0,0,-speed*Time.deltaTime));//小球顺时针旋转
	}
}

学习完了这个小项目,觉得自己在学习完之后的总结上仍有欠缺,如果大家有什么好的意见,希望可以多多指教

你可能感兴趣的:(siki学习项目)