Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码
【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili
Crystal_Skill_Controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Crystal_Skill_Controller : MonoBehaviour
{
private Animator anim => GetComponent<Animator>();
private CircleCollider2D cd => GetComponent<CircleCollider2D>();
private float crystalExitTimer;
private bool canExplode;
private bool canMove;
private float moveSpeed;
private bool canGrow;
private float growSpeed = 5;
public void SetupCrystal(float _crystalDuration,bool _canExplode,bool _canMove,float _moveSpeed)
{
crystalExitTimer = _crystalDuration;
canExplode = _canExplode;
canMove = _canMove;
moveSpeed = _moveSpeed;
}
private void Update()
{
crystalExitTimer -= Time.deltaTime;
if (crystalExitTimer < 0)
{
FinishCrystal();
}
if (canGrow)
transform.localScale = Vector2.Lerp(transform.localScale, new Vector2(3, 3), growSpeed * Time.deltaTime);
}
private void AnimationExplodeEvent()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, cd.radius);
foreach(var hit in colliders)
{
if (hit.GetComponent<Enemy>() != null)
hit.GetComponent<Enemy>().Damage();
}
}
public void FinishCrystal()
{
if (canExplode)
{
canGrow = true;
anim.SetBool("Explode",true);
}
else
{
SelfDestory();
}
}
public void SelfDestory() => Destroy(gameObject);
}
Crystal_Skill
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Crystal_Skill : Skill
{
[SerializeField] private GameObject crystalPrefab;
[SerializeField] private float crystalDuration;
private GameObject currentCrystal;
[Header("Explosive crystal")]
[SerializeField] private bool canExplode;
[Header("Moving crystal")]
[SerializeField] private bool canMoveToEnemy;
[SerializeField] private float moveSpeed;
public override bool CanUseSkill()
{
return base.CanUseSkill();
}
public override void UseSkill()
{
base.UseSkill();
if (currentCrystal == null)
{
currentCrystal = Instantiate(crystalPrefab, player.transform.position, Quaternion.identity);
Crystal_Skill_Controller currentCrystalScripts = currentCrystal.GetComponent<Crystal_Skill_Controller>();
currentCrystalScripts.SetupCrystal(crystalDuration,canExplode,canMoveToEnemy,moveSpeed);
}
else
{
Vector2 playerPos = player.transform.position;
player.transform.position = currentCrystal.transform.position;
currentCrystal.transform.position = playerPos;
currentCrystal.GetComponent<Crystal_Skill_Controller>()?.FinishCrystal();
}
}
protected override void Start()
{
base.Start();
}
protected override void Update()
{
base.Update();
}
}