Article directory
The final effect of this section
Preface
This section follows the previous one and mainly implements different game interfaces and different character selections.
Start game
Simple drawing UI
Modified default disables along with other operations, including player enemies and enemy spawners, and game countdown
if(!GameManager.instance.isLive) return;
Modify GameManager
public void GameStart()
{
health = maxHealth;
uiLevelUp.Select(0);
Resume();
}
Add click event
Effect
The protagonist loses blood and dies
Modify Player
void OnCollisionStay2D(Collision2D collision)
{
// 如果玩家已经死亡,则不执行接下来的逻辑
if (!GameManager.instance.isLive)
{
return;
}
// 对玩家的生命值进行递减
GameManager.instance.health -= Time.deltaTime * 10;
// 如果玩家生命值小于0,则进行相应处理
if (GameManager.instance.health < 0)
{
// 关闭当前物体所有子对象的渲染
for (int index = 0; index < transform.childCount; index++)
{
transform.GetChild(index).gameObject.SetActive(false);
}
// 播放死亡动画
animator.SetTrigger("Dead");
}
}
Effect
game over
Simplified UI
Modified GameManager
public GameObject uiResult;
public void GameOver()
{
StartCoroutine(GameOverRoutine());
}
IEnumerator GameOverRoutine()
{
// 设置玩家为死亡状态
isLive = false;
// 等待0.5秒
yield return new WaitForSeconds(0.5f);
uiResult.SetActive(true);
// 停止游戏逻辑或重置游戏状态
Stop();
}
public void GameRetry()
{
// 重新加载场景(假设索引为0的场景是起始场景)
SceneManager.LoadScene(0);
}
Modify Player
// 播放死亡动画
animator.SetTrigger("Dead");
GameManager.instance.GameOver();
Bind click event
Effect
Clear screen effect
The function of reusing bullets is equivalent to generating a bullet with super high damage and super wide range.
Win the competition
Draw the UI and reuse the game end UI.
Modify GameManager
public GameObject uiVictroy;
public GameObject enemyCleaner;
void Update()
{
//。。。
if (gameTime >= maxGameTime){
gameTime = maxGameTime;
GameVictroy();
}
}
public void GetExp()
{
//胜利了不需要经验
if (!GameManager.instance.isLive) return;
//。。。
}
public void GameVictroy()
{
StartCoroutine(GamevictroyRoutine());
}
IEnumerator GamevictroyRoutine()
{
// 设置玩家为死亡状态
isLive = false;
// 激活敌人清除器游戏对象
enemyCleaner.SetActive(true);
// 等待0.5秒
yield return new WaitForSeconds(0.5f);
// 激活 UI 结果游戏对象
uiVictroy.SetActive(true);
// 停止游戏逻辑或重置游戏状态
Stop();
}
Configuration parameters
Effect
Character selection interface
Draw the UI and change the start game button in front
Modify logic GameManager
public int playerId;
public void GameStart(int id)
{
playerId = id;
health = maxHealth;
player.gameObject.SetActive(true);
uiLevelUp.Select(playerId % 2);
Resume();
}
Modify Player to implement character animation switching
public RuntimeAnimatorController[] animCon;
void Awake()
{
spriter = GetComponent<SpriteRenderer>();
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
scanner = GetComponent<Scanner>();
hands = GetComponentsInChildren<Hand>();
}
private void OnEnable() {
animator.runtimeAnimatorController = animCon[GameManager.instance.playerId];
}
For new character animation, just create a new animation controller overlay
Configure different animation parameters and hide the character by default
Modify the click event and configure Different role ids
Effect
Each character has its own characteristics and different initial attributes.
AddCharacter
public class Character : MonoBehaviour
{
// 玩家速度
public static float Speed
{
get
{
return GameManager.instance.playerId == 0 ? 1.1f : 1f;
}
}
// 武器速度
public static float WeaponSpeed
{
get
{
return GameManager.instance.playerId == 1 ? 1.1f : 1f;
}
}
// 武器射速
public static float WeaponRate
{
get
{
return GameManager.instance.playerId == 1 ? 0.9f : 1f;
}
}
// 伤害值
public static float Damage
{
get
{
return GameManager.instance.playerId == 2 ? 1.2f : 1f;
}
}
// 数量
public static int Count
{
get
{
return GameManager.instance.playerId == 3 ? 1 : 0;
}
}
}
Modify Player
private void OnEnable() {
speed *= Character.Speed;
animator.runtimeAnimatorController = animCon[GameManager.instance.playerId];
}
Modify Gear
void RateUp()
{
Weapon[] weapons = transform.parent.GetComponentsInChildren<Weapon>(); // 获取所有子物体中的武器组件
foreach (Weapon weapon in weapons)
{
switch (weapon.id)
{
case 0:
float speed = 150 * Character.WeaponSpeed;
weapon.speed = speed + speed * rate;
break;
default:
speed = 0.5f * Character.WeaponRate;
weapon.speed = speed * (1f - rate);
break;
}
}
}
void SpeedUp()
{
float speed = 3.0f * Character.Speed;
GameManager.instance.player.speed = speed * (1.0f + rate);
}
Modify Weapon
public void LevelUp(float damage, int count)
{
this.damage = damage * Character.Damage;
this.count += count;
if (id == 0) Batch();
// player.BroadcastMessage("ApplyGear");
}
public void Init(ItemData data)
{
//。。。
damage = data.baseDamage * Character.Damage; // 设置物品基础伤害
count = data.baseCount + Character.Count; // 设置物品基础数量
for (int index = 0; index < GameManager.instance.pool.prefabs.Length; index++)
{
if (data.projectile == GameManager.instance.pool.prefabs[index])
{
prefabId = index; // 设置投射物的预制体ID
break;
}
}
switch (id)
{
case 0:
speed = 150 * Character.WeaponSpeed; // 设置武器旋转的速度
Batch();
break;
default:
speed = 0.5f * Character.WeaponRate;
break;
}
//。。。
}
Effect: Little Blue has high movement speed and Little Red has fast shooting speed.
reference
【Video】https://www.youtube.com/watch?v=MmW166cHj54&list=PLO-mt5Iu5TeZF8xMHqtT_DhAPKmjF6i3x
Source code
Source code is in the last section
end
Giving roses to others will leave a lingering fragrance in your hands! If the content of the article is helpful to you, please don't be stingy with your 点赞评论和关注
so that I can receive feedback as soon as possible. Every time you 支持
The greatest motivation for creation. Of course, if you find 存在错误
or 更好的解决方法
in the article, you are welcome to comment and send me a private message!
Good, I am向宇
,https://xiangyu.blog.csdn.net
A developer who has been working quietly in a small company recently started studying Unity by himself out of interest. If you encounter any problems, you are also welcome to comment and send me a private message. Although I may not necessarily know some of the questions, I will check the information from all parties and try to give the best suggestions. I hope to help more people who want to learn programming. People, encourage each other~