见缝插针
**
加粗样式
**
1.创建2D项目,导入素材 将圆拖入Hierarchy面板。
2. 写个代码让圆旋转,命名为Rotate,将脚本挂到圆的身上。
3. 代码如下;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
public float speed = 100;//圆旋转速度
// 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));
}
}
4.创建两个空物体(Create Empty)命名为Inspoint (生成针的位置)和Startpoint针等待位置。自己调位置。将Select icon设置成灰色。
5.将针拖到层级面板中,添加Rigbody2D,将重力(Gravity)设置为0。
6.创建新代码,命名为Pin,代码挂载到pin上。代码如下;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pin : MonoBehaviour
{
public bool isReah = false;
public bool isFly = false;
private Transform starPoint;
private Transform dunPos;//圆的位置
public float speed = 5;//针移动的速度
private Vector3 endPoint;//结束位置
// Start is called before the first frame update
void Start()
{
starPoint = GameObject.Find(“StartPoint”).transform;
dunPos = GameObject.Find(“dun”).transform;
endPoint = dunPos.position;
endPoint.y -= 4f;
}
// Update is called once per frame
void Update()
{
if (isFly == false)
{
if (isReah == false)
{
transform.position = Vector3.MoveTowards(transform.position, starPoint.position, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, starPoint.position) < 0.05f)
{
isReah = true;
}
}
}
else
{
transform.position = Vector3.MoveTowards(transform.position, endPoint, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, endPoint) < 0.05)
{
isFly = false;
transform.parent = dunPos;
}
}
}
public void StartFly()
{
isFly = true;
isReah = true;
}
}
7.创建针的预设体,直接把层级面板中的Pin拖到Prafabs里。
8.创建空物体命名为,GameManger。创建Text ,拖到圆中心。
9.创建代码命名为GameManger,把代码挂到GameManger身上。代码如下;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManger : MonoBehaviour
{
public GameObject PinPrefab;//针的预设物
private Transform insPoint;//实例化位置
public Pin currentPin;//当前针
public bool isover;
public GameObject dun;
public Text scoreText;//分数显示的文本
public int score = 0;//分数
public Camera mainCamera;
public int speed = 3;//渐变的速度
// Start is called before the first frame update
void Start()
{
insPoint = GameObject.Find(“InsPoint”).transform;
InsPin();
mainCamera = Camera.main;//获取主相机
}
// Update is called once per frame
void Update()
{
if (isover) return;
//获取鼠标点击 0-鼠标左键 1-鼠标右键 2-鼠标中键
if (Input.GetMouseButtonDown(0))
{
score++;
scoreText.text = score.ToString();
currentPin.StartFly();
//Debug.Log("鼠标左键点击了一下");
InsPin();
}
}
internal static object Find(string v)
{
throw new NotImplementedException();
}
//实例化针
void InsPin()
{
currentPin = GameObject.Instantiate(PinPrefab, insPoint.position, PinPrefab.transform.rotation).GetComponent<Pin>();
}
public void GameOver()
{
if (isover) return;
dun.GetComponent<Rotate>().enabled = false;
StartCoroutine(GameOverAnimation());//开启协程
isover = 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.01)
{
break;
}
yield return 0;
}
yield return new WaitForSeconds(2);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
10.回到unity中,将针的预制体拖到Pin 拖到prefab里,将圆拖入dun中,text拖入Score Text 中,相机拖入Main cemera中。
11.游戏结束,创建代码PinHead,将他挂到针的身上。代码如下;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PinHead : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == “jian”)
{
GameObject.Find(“GameManger”).GetComponent().GameOver();
//游戏结束
}
}
}
10.回到unity中,选中针的预制体点击Add Component,添加Circle collider2d组件并勾选Is Tirgger。点击Edit Collider在Scene视图中将碰撞体缩放到针的末尾。然后点击Apply应用。然后创建一个名为“jian”的tag(标签)。
11运行游戏就完成了。大家要细心看好每一步哦!