2D游戏
2D游戏
先创建
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
//准备发射的位置
//public Transform startpos;
//分数变量
private int score = 0;
//分数Text文本UI
public Text scoretext;
//预制体生成的位置
public Transform pinpos;
//预制体
public GameObject pinprefab;
//创建Pin脚本的成员
private Pin FirePin;
//单例(在其他脚本里可以用_instance点出该脚本的函数和成员变量)
public static GameManager _instance;
//游戏结束开关
private bool IsGameOver = false;
//摄像机
private Camera maincamera;
private void Awake()
{
//单例模式
_instance = this;
}
// Start is called before the first frame update
void Start()
{
//startpos = GameObject.Find("StarPos").transform;
//得到生成位置PinPos的位置
pinpos = GameObject.Find("PinPos").transform;
//获取摄像机
maincamera = Camera.main;
//实例化预制体
AddPin();
}
// Update is called once per frame
void Update()
{
//如果IsGameOver开关为true就return掉,不执行后面的逻辑
if (IsGameOver==true)
return;
//鼠标左键按下
if (Input.GetMouseButtonDown(0))
{
//分数增加
score++;
//分数显示文本内容为score
scoretext.text = score.ToString();
//调用Pin脚本里的StarFly函数
FirePin.StarFly();
//实例化预制体
AddPin();
}
}
void AddPin()
{
//预制体实例化
FirePin = GameObject.Instantiate(pinprefab, pinpos.position, pinprefab.transform.rotation).GetComponent<Pin>();
}
public void GameOver()
{
//如果IsGameOver为true就return掉,不执行后面的逻辑
if (IsGameOver)
return;
//让Rotation脚本不可用(不执行--旋转)
GameObject.Find("Circle").GetComponent<Rotation>().enabled = false;
//游戏结束开关打开,并执行协程
IsGameOver = true;
StartCoroutine("GameOverAnimation");
}
//协程,播放游戏结束页面缓动动画并重新加载场景开始游戏
IEnumerator GameOverAnimation()
{
while (true)
{
//摄像机背景色的差值运算
maincamera.backgroundColor = Color.Lerp(maincamera.backgroundColor, Color.gray, 3 * Time.deltaTime);
//摄像机的orthographicSize的差值运算
maincamera.orthographicSize = Mathf.Lerp(maincamera.orthographicSize, 3, 3 * Time.deltaTime);
//当Size到达3时就中断这个while循环
if (Mathf.Abs(maincamera.orthographicSize - 3) <= 0.02f)
{
break;
}
//等待一帧
yield return 0;
}
//等待两秒之后加载场景重新开始游戏
yield return new WaitForSeconds(2);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
private bool IsFly = false;
private bool IsReady = false;
private Transform startpos;
private Transform circle;
public float speed = 15f;
private Vector3 targetpos;
// Start is called before the first frame update
void Start()
{
startpos = GameObject.Find("StarPos").transform;
circle = GameObject.Find("Circle").transform;
targetpos =new Vector3(circle.position.x, 0, circle.position.z);
}
// Update is called once per frame
void Update()
{
if (IsFly==false)
{
if (IsReady==false)
{
transform.position = Vector3.MoveTowards(transform.position, startpos.position, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, startpos.position) <= 0.05f)
{
IsReady = true;
}
}
}
else
{
transform.position = Vector3.MoveTowards(transform.position, targetpos, speed * Time.deltaTime);
if(Vector3.Distance(transform.position,targetpos)<=0.03f)
{
transform.position = targetpos;
IsFly = false;
transform.parent = circle;
}
}
}
public void StarFly()
{
IsFly = true;
IsReady = true;
}
public class Pin : MonoBehaviour
{
private bool IsFly = false;
private bool IsReady = false;
private Transform startpos;
public float speed = 5f;
// Start is called before the first frame update
void Start()
{
startpos = GameObject.Find("StarPos").transform;
}
// Update is called once per frame
void Update()
{
if (IsFly==false)
{
if (IsReady==false)
{
transform.position = Vector3.MoveTowards(transform.position, startpos.position, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, startpos.position) <= 0.05f)
{
IsReady = true;
}
}
}
}
}
public class Rotation : MonoBehaviour
{
public float speed = 90f;
// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(0, 0, speed * Time.deltaTime));
}
}
##大幅度
#大法师
让圆形转起来,思路,其实是围绕Z轴旋转,
public float speed = 90; // 这里是旋转的度数
void Update() {
transform.Rotate(new Vector(0, 0 , speed *Time.deltaTime));
}
transform.Rotate(new Vector(0,0));
导入圆后更改参数
li hNiu
private Transform startPoint;
private Transform spawPoint;
1.transform.
public class RotateSelf : MonoBehaviour
{
public float speed = 90;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Rotate(new Vector3(0, 0, -speed * Time.deltaTime));
}
}
给针添加脚本
1. 创建C#=脚本 Pin
1. 把pin放到Hierarchy视图中
调节大小 跟圆的一半差不多
颜色设置为黑色
z轴旋转90度
放在0,0,0位置
2.重新拖放一个圆形 放在circle的下面作为一个整体
调节大小
3.整体放在Project 的Prefabs下面
4.针头添加碰撞器
add Component -> Physics 2D -> circle Colider 2D
点击 Prefabs apply
1.创建旋转小球
把小球图片放进Hierarchy视图
位置 大小调整 颜色为黑色
位置要靠上
2.分数显示
使用UI-》text显示分数
设置一下UI:
删除eventsystem text位置reset
字体上下左右居中、颜色白色、字体大小修改为88,初始内容0
Canvas渲染模式设置为Word Space
修改canvas和text大小缩放设置小一点
把canvas放到circle的中心
Canvas 摄像机设置为maincamera
Canvas 放在circle下面, 分数不会旋转
在StartFly里面isReach必须设为true
否则同时满足以下条件,会出现bug:1 针未就绪;2 点击鼠标左键
给针添加脚本 Pin
pin拿过去
private bool isFly = false;
private bool isReach=false;
private Transform startPoint;
startPoint=GameObject.Find("StartPoint").transform;
if(isFly==false)
{
if(is Rea