视频
子弹脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FLProjectile : MonoBehaviour
{
[SerializeField]
float speed = 10f;
[SerializeField]
float rotateSpeed = 0.5f;
Vector2 dir;
GameObject[] enemys;
[SerializeField]
Transform enemyPos;
TrailRenderer trail;
BoxCollider2D collider;
private void Awake()
{
trail = GetComponentInChildren<TrailRenderer>();
collider = GetComponent<BoxCollider2D>();
collider.isTrigger = true;
}
private void OnEnable()
{
enemys = GameObject.FindGameObjectsWithTag("Enemy");
enemyPos = GetTargertEnemyPos();
StartCoroutine(MoveDirectly());
}
private void OnDisable()
{
trail.Clear();
}
Transform GetTargertEnemyPos()
{
if (enemys.Length != 0)
return enemys[Random.Range(0, enemys.Length)].transform;
else return null;
}
//曲线移动
IEnumerator MoveDirectly()
{
while (gameObject.activeSelf)
{
if (enemyPos != null)
transform.up = Vector3.Slerp(transform.up, enemyPos.position - transform.position,
rotateSpeed / Vector2.Distance(this.transform.position, enemyPos.position));
transform.position += transform.up * speed * Time.deltaTime;
yield return null;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
gameObject.SetActive(false);//协成依赖于mono的Update,物体禁用后协成也不再执行
}
}
角色控制部分
private void Update()
{
if (power >= 100)//能量达到100执行
{
StartCoroutine(nameof(StartRangeFire));
power = 0;
}
}
IEnumerator StartRangeFire()
{
StartCoroutine(nameof(RFire));//开启曲线子弹
yield return new WaitForSeconds(RFireTime);//设定时间后
StopCoroutine(nameof(RFire));//结束
StartCoroutine(nameof(DesShiled));//护盾消失
}
IEnumerator RFire()
{
//判断护盾是否显示
if (!shiled.gameObject.activeSelf)//未显示则,则显示护盾
{
shiled.material.SetColor("_FresnelColor", Color.red * 2.5f);
shiled.gameObject.SetActive(true);
shiled.gameObject.transform.DOScale(Vector3.one*100, 0.5f);
}
else//显示则改变护盾颜色
shiled.material.SetColor("_FresnelColor", Color.red * 2.5f);
while (true)
{
PoolManager.Release(projectile4, pointR.position, pointR.rotation);
PoolManager.Release(projectile4, pointR2.position, pointR2.rotation);
yield return Rtime;
}
}
//护盾消失
IEnumerator DesShiled()
{
while (true)
{
t += Time.deltaTime;//rtime时间内r变为1
shiled.material.SetFloat("_BurnAmount", t);
yield return null;
if(t>1)
{
t = 0;
break;
}
}
shiled.gameObject.SetActive(false);
shiled.material.SetFloat("_BurnAmount", 0);
shiled.material.SetColor("_FresnelColor", shiledIdColor);
shiled.gameObject.transform.localScale = Vector3.zero;
StopCoroutine(nameof(DesShiled));
}