个人主页:@元宇宙-秩沅
hallo 欢迎 点赞 收藏⭐ 留言 加关注✅!
本文由 秩沅 原创
收录于专栏:unityUI专题篇
️
️:步骤实现
血条核心代码
//GUI生命函数
private void OnGUI()
{
if(showTime >=0) //血条倒计时展示
{
//简单的倒计时
showTime -= Time.deltaTime;
//世界坐标转屏幕坐标
SreecPosition = Camera.main.WorldToScreenPoint(transform.position);
//屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
SreecPosition.y = Screen.height - SreecPosition.y;
//底图Rect位置参数赋值
GUIPosition1.x = SreecPosition.x - 50;
GUIPosition1.y = SreecPosition.y - 70;
GUIPosition1.width = 100;
GUIPosition1.height = 15;
//血条图Rect位置参数赋值
GUIPosition2.x = SreecPosition.x - 50;
GUIPosition2.y = SreecPosition.y - 70;
//血条长度和血量同步
GUIPosition2.width = 100*(nowBlood/maxBlood);
GUIPosition2.height = 15;
//绘制底图
GUI.DrawTexture(GUIPosition1, underTexture);
//绘制血条
GUI.DrawTexture(GUIPosition2, topTexture);
}
}
更新敌方坦克
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能:敌方坦克逻辑封装
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class EnemyTank:TankFather
{
//随机移动点的位置数组
public Transform[] randomPosition;
//目标位置
private Transform target;
//目标坦克
public Transform Player;
//检测范围
public float distance = 10f;
//子弹发射参数
public AudioSource shootMusic; //发射音效
public GameObject shootball; //发射的子弹类型
public float shootSpeed = 1000f; //子弹的速度
public Transform[] shootTransform; //发射的组件信息
public bool shootSwitch = false;
private float endTime = 3f; //子弹发射间隔的时间
//血条的参数
public Texture underTexture;
public Texture topTexture;
private Vector3 SreecPosition;
private Rect GUIPosition1;
private Rect GUIPosition2;
public float showTime = 0;
private void Start()
{
//属性初始化赋值
maxBlood = 500;
nowBlood = 500;
attack = 30;
HeadSpeed = 50;
//射击音效关闭
shootMusic.enabled = false;
moveSpeed = 10f;
//先随机整一个目标点
RandomPosition();
}
private void Update()
{
transform.LookAt(target);
//始终超自己的正方向移动
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); //当距离差不多相等时,再随机目标点
if(Vector3.Distance(transform .position ,target .position)< 0.5f)
{
RandomPosition();
}
//范围检测
if (Vector3 .Distance(transform .position ,Player.position )<= distance && Player !=null )
{
//炮口瞄准玩家
Head.transform.LookAt(Player);
//倒计时发射子弹
endTime = Mathf.MoveTowards(endTime, 0, 0.1f);
if (endTime <= 0)
{
Fire();
endTime = 3f;
}
}
}
//随机指向一个移动点
public void RandomPosition()
{
if (randomPosition.Length != 0)
target = randomPosition[Random.Range(0, randomPosition.Length)];
}
//触发检测
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "bullet" )
{
//添加子弹挂载的目标坦克,方便获取该子弹的攻击力,与受伤逻辑相结合
BulletMove ball = other.gameObject.GetComponent<BulletMove>();
TankFather ballTank = ball.Tank.GetComponent<TankFather>();
//当子弹不是自己打的到自己身上的时候
if (ballTank.tag != gameObject.tag)
{
//扣血
Heart(ballTank);
}
}
}
public override void Fire()
{
//开启射击音效
shootMusic.enabled = true;
shootMusic.Play();
for (int i = 0; i < shootTransform.Length ; i++)
{
GameObject ball = Instantiate(shootball, shootTransform[i].position, shootTransform[i].rotation);
BulletMove movScript = ball.GetComponent<BulletMove>();
Rigidbody shootBall = ball.GetComponent<Rigidbody>();
shootBall.AddForce(shootTransform[i].transform.forward * shootSpeed);
movScript.Tank = gameObject; //声明子弹是由谁打出去的
}
}
//死亡行为重写
public override void Death()
{
base.Death();
GamePlane.SingleInstance.AddScore(10);
}
//受伤行为重写
public override void Heart(TankFather other)
{
base.Heart(other);
showTime = 3; //血条倒计时展示
}
//GUI生命函数
private void OnGUI()
{
if(showTime >=0) //血条倒计时展示
{
//简单的倒计时
showTime -= Time.deltaTime;
//世界坐标转屏幕坐标
SreecPosition = Camera.main.WorldToScreenPoint(transform.position);
//屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
SreecPosition.y = Screen.height - SreecPosition.y;
//底图Rect位置参数赋值
GUIPosition1.x = SreecPosition.x - 50;
GUIPosition1.y = SreecPosition.y - 70;
GUIPosition1.width = 100;
GUIPosition1.height = 15;
//血条图Rect位置参数赋值
GUIPosition2.x = SreecPosition.x - 50;
GUIPosition2.y = SreecPosition.y - 70;
//血条长度和血量同步
GUIPosition2.width = 100*(nowBlood/maxBlood);
GUIPosition2.height = 15;
//绘制底图
GUI.DrawTexture(GUIPosition1, underTexture);
//绘制血条
GUI.DrawTexture(GUIPosition2, topTexture);
}
}
}
更新炮台
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能:敌方坦克逻辑封装
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class EnemyTank:TankFather
{
//随机移动点的位置数组
public Transform[] randomPosition;
//目标位置
private Transform target;
//目标坦克
public Transform Player;
//检测范围
public float distance = 10f;
//子弹发射参数
public AudioSource shootMusic; //发射音效
public GameObject shootball; //发射的子弹类型
public float shootSpeed = 1000f; //子弹的速度
public Transform[] shootTransform; //发射的组件信息
public bool shootSwitch = false;
private float endTime = 3f; //子弹发射间隔的时间
//血条的参数
public Texture underTexture;
public Texture topTexture;
private Vector3 SreecPosition;
private Rect GUIPosition1;
private Rect GUIPosition2;
public float showTime = 0;
private void Start()
{
//属性初始化赋值
maxBlood = 500;
nowBlood = 500;
attack = 30;
HeadSpeed = 50;
//射击音效关闭
shootMusic.enabled = false;
moveSpeed = 10f;
//先随机整一个目标点
RandomPosition();
}
private void Update()
{
transform.LookAt(target);
//始终超自己的正方向移动
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); //当距离差不多相等时,再随机目标点
if(Vector3.Distance(transform .position ,target .position)< 0.5f)
{
RandomPosition();
}
//范围检测
if (Vector3 .Distance(transform .position ,Player.position )<= distance && Player !=null )
{
//炮口瞄准玩家
Head.transform.LookAt(Player);
//倒计时发射子弹
endTime = Mathf.MoveTowards(endTime, 0, 0.1f);
if (endTime <= 0)
{
Fire();
endTime = 3f;
}
}
}
//随机指向一个移动点
public void RandomPosition()
{
if (randomPosition.Length != 0)
target = randomPosition[Random.Range(0, randomPosition.Length)];
}
//触发检测
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "bullet" )
{
//添加子弹挂载的目标坦克,方便获取该子弹的攻击力,与受伤逻辑相结合
BulletMove ball = other.gameObject.GetComponent<BulletMove>();
TankFather ballTank = ball.Tank.GetComponent<TankFather>();
//当子弹不是自己打的到自己身上的时候
if (ballTank.tag != gameObject.tag)
{
//扣血
Heart(ballTank);
}
}
}
public override void Fire()
{
//开启射击音效
shootMusic.enabled = true;
shootMusic.Play();
for (int i = 0; i < shootTransform.Length ; i++)
{
GameObject ball = Instantiate(shootball, shootTransform[i].position, shootTransform[i].rotation);
BulletMove movScript = ball.GetComponent<BulletMove>();
Rigidbody shootBall = ball.GetComponent<Rigidbody>();
shootBall.AddForce(shootTransform[i].transform.forward * shootSpeed);
movScript.Tank = gameObject; //声明子弹是由谁打出去的
}
}
//死亡行为重写
public override void Death()
{
base.Death();
GamePlane.SingleInstance.AddScore(10);
}
//受伤行为重写
public override void Heart(TankFather other)
{
base.Heart(other);
showTime = 3; //血条倒计时展示
}
//GUI生命函数
private void OnGUI()
{
if(showTime >=0) //血条倒计时展示
{
//简单的倒计时
showTime -= Time.deltaTime;
//世界坐标转屏幕坐标
SreecPosition = Camera.main.WorldToScreenPoint(transform.position);
//屏幕坐标转GUI坐标_____GUI的原点在左上角,屏幕坐标原点在左下角
SreecPosition.y = Screen.height - SreecPosition.y;
//底图Rect位置参数赋值
GUIPosition1.x = SreecPosition.x - 50;
GUIPosition1.y = SreecPosition.y - 70;
GUIPosition1.width = 100;
GUIPosition1.height = 15;
//血条图Rect位置参数赋值
GUIPosition2.x = SreecPosition.x - 50;
GUIPosition2.y = SreecPosition.y - 70;
//血条长度和血量同步
GUIPosition2.width = 100*(nowBlood/maxBlood);
GUIPosition2.height = 15;
//绘制底图
GUI.DrawTexture(GUIPosition1, underTexture);
//绘制血条
GUI.DrawTexture(GUIPosition2, topTexture);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能:通关面板逻辑相关
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class VictorPlane : BeginFather<VictorPlane>
{
public Button Require; //确认按钮
public InputFram name; //输入的昵称
public Label Score; //显示的分数
public Label LTime; //显示的时间
private int time;
private void Start()
{
//添加按钮事件
Require.triggerEvent += () => {
//点击按钮后跳到主场景
SceneManager.LoadScene("BeginScreen");
//将数据存储
SaveGameData.DataContorl.SaveRankData(name.ContorlContent.text, Convert.ToInt32(Score.ContorlContent.text), time);
};
//默认隐藏
Hidden();
}
private void Update()
{
//将光标显示出来
Cursor.visible = true;
//分数显示
Score.ContorlContent.text = GamePlane.SingleInstance.laScore.ContorlContent.text;
//时间显示
LTime.ContorlContent.text = GamePlane.SingleInstance.laTime.ContorlContent.text;
//时间
time = GamePlane.SingleInstance.time;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目:
//___________功能: 通关触发
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class over : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag ("Player") )
{
Time.timeScale = 0;
VictorPlane.SingleInstance.Show();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 死亡显示面板
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class DiedPlane : BeginFather<DiedPlane >
{
public Button back;
private void Start()
{
back.triggerEvent += () =>
{
//点击按钮后跳到主场景
SceneManager.LoadScene("BeginScreen");
};
Hidden();
}
}
⭐相关文章⭐
⭐【2023unity游戏制作-mango的冒险】-6.关卡设计
⭐【2023unity游戏制作-mango的冒险】-5.攻击系统的简单实现
⭐【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随
⭐【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现
⭐【2023unity游戏制作-mango的冒险】-2.始画面API制作
⭐【2023unity游戏制作-mango的冒险】-1.场景搭建
⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)
⭐本站最全-unity常用API大全(万字详解),不信你不收藏
你们的点赞 收藏⭐ 留言 关注✅是我持续创作,输出优质内容的最大动力!