unity(1)StickPin

RotateSelf.cs
控制圆靶旋转

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));
    }
}

Vector3是值类型,transform是引用类型

Pin.cs

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

public class Pin : MonoBehaviour
{
    public float speed = 20;
    private bool isFly = false;//是否移动
    private bool isReach = false;//是否到达预设位置
    private Transform StartPoint;//预设位置

    private Vector3 targetCirclePos;//移动目标位置
    private Transform circle;//靶位置
    // Start is called before the first frame update
    void Start()
    {
        StartPoint = GameObject.Find("StartPoint").transform;
        circle = GameObject.Find("Circle").transform;
        targetCirclePos = circle.position;
        targetCirclePos.y -= 0.9f;
    }

    // Update is called once per frame
    void Update()
    {
        if(isFly == false)
        {
            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;
                }
            }
        }
        else
        {
            //if(Vector3.Distance(transform.position, circle.position) >= 0.05f)
            //{
                transform.position = Vector3.MoveTowards(transform.position, targetCirclePos, 70 * speed * Time.deltaTime);
           // }
            
            if(Vector3.Distance(transform.position,targetCirclePos) < 0.05f)
            {
                transform.position = targetCirclePos;//准确定位
                transform.parent = circle;//父组件
                isFly = false;
            }
        }

    }
    public void StartFly()
    {
        isFly = true;
        isReach = true;
    }
}

Vector2和Vector3相同方法不同的计算方式
PinHit.cs

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

public class PinHit : MonoBehaviour
{
    // Start is called before the first frame update
    private void OnTriggerEnter2D(Collider2D collision)//触发器
    {
        if(collision.tag =="PinHead")
        {
            GameObject.Find("GameManager").GetComponent<GameManager>().GameOver();
        }
    }
   
}

GameManager.cs

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;
    private bool isGameOver = false;//游戏进程控制
    private int score = 0;//UI分数
    private Camera maincamera;//主视角设定
    public Text scoreText;
    private Pin currentPin;//当前针

    public GameObject  PinPrefab;//编辑器内设置预设体
    public float speed = 3;//视角变化速度
    // Start is called before the first frame update
    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();//unity的值类型全部值类型都是派生自抽象类型System.ValueType
            currentPin.StartFly();
            SpawnPin();
            

        }
    }
    void SpawnPin()
    {
     
        currentPin = GameObject.Instantiate(PinPrefab, SpawnPoint.position, PinPrefab.transform.rotation).GetComponent<Pin>();//预设体初始化方法
    }

    public void GameOver()
    {
        if (isGameOver) return;
        GameObject.Find("Circle").GetComponent<RotateSelf>().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(1);
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

游戏管理器,控制输入输出和游戏进程

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页