八.方格可放置炮塔
给敌人加了血条,稍微改了代码
就是为啥我的血条随着敌人转,教程里的不会转啊...
c#node
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Node : MonoBehaviour
{
Renderer render;
Color initColor;
public GameObject turretPrefab;
void Start()
{
render = GetComponent<MeshRenderer>();
initColor = render.material.color;
}
void Update()
{
}
private void OnMouseEnter()
{
render.material.color = Color.white;
}
private void OnMouseExit()
{
render.material.color = initColor;
}
private void OnMouseDown()
{
Debug.Log("放置炮塔");
Instantiate (turretPrefab, transform.position, Quaternion.identity);
}
}
发现放置后的炮塔想发射子弹时报错,当时脚本的子弹预制体是unity里放上的...可教程没出错啊
破案了,加载预制体后忘覆盖了
炮塔位置
我的格子上面还有个父物体,比教程还难一点
Vector3 pos = new Vector3(-0.65f, 0.444f, -0.4f);加上这个偏移量(基础不过关就这一条代码卡了有一会)
九.增加money和timer的ui
视频ui用的text文本是旧版的,新版的默认是TextMeshProUGUI,要加using TMPro;,往Enemy_Health的Damage(float amount)中的判断打死里加钱(我定的10,炮塔默认花费100)
再设置结束画面的UI(中间我以为多了个EventSystem,删掉了...寄)重加回来;结束画面加一个按钮,重来,但发现场景重加载后不生怪了.
猜测是游戏结束时怪没死完,怪存活数量不为零,自然不生怪,改之,可行
十.加上游戏结束ui和暂停ui
c#游戏结束
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class GameOver : MonoBehaviour
{
public TextMeshProUGUI RoundsText;
public PauseUI PauseUI;
private void OnEnable()
{
StartCoroutine(ShowRoundsText());
}
public void Retry()
{
Debug.Log("Re");
SceneManager.LoadScene(0);
PauseUI.SwitchUI();
}
IEnumerator ShowRoundsText()
{
int rounds =0;//记录当前值
RoundsText.text = rounds.ToString();
yield return new WaitForSeconds(0.5f);
while(rounds < PlayerStatus.Rounds)
{
rounds++;
RoundsText.text = rounds.ToString();
yield return new WaitForSeconds(0.07f);
}
}
}
C#暂停ui
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class PauseUI : MonoBehaviour
{
public TextMeshProUGUI RoundsText;
public GameObject ui;
private void OnEnable()
{
StartCoroutine(ShowRoundsText());
}
public void GoAhead()
{
Debug.Log("Go ahead");
SwitchUI();
//SceneManager.LoadScene(0);
}
IEnumerator ShowRoundsText()
{
int rounds = 0;//记录当前值
RoundsText.text = rounds.ToString();
yield return new WaitForSeconds(0.5f);
while (rounds < PlayerStatus.Rounds)
{
rounds++;
RoundsText.text = rounds.ToString();
yield return new WaitForSeconds(0.07f);
}
}
public void SwitchUI()
{
ui.SetActive(!ui.activeSelf);
if (ui.activeSelf)//暂停
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
}
暂停界面的重来我偷懒直接用结束里的函数就导致重来时游戏是暂停的,就往后面加了PauseUI.SwitchUI();
游戏的雏形整完了,先交了任务,更多的以后补(大雾)