p59 Counter attack - Enemy‘s Stun State ,类恶魔城学习记录

Counter attack - Enemy‘s Stun State ,p=59-类恶魔城学习

新加主功能一、怪物昏迷状态(类似收击)

实现:在enemy.cs设置变量 昏迷距离(vector)、昏迷时间(float)

[Header("Stun Info")]
public float stunDuration;
public Vector2 stunDistance;
昏迷状态脚本的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkeletonStunState : SkeletonState
{
    public SkeletonStunState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName, Enemy_Skeleton _enemy)
        : base(_enemyBase, _stateMachine, _animBoolName, _enemy)
    {
    }

    public override void Enter()
    {
        base.Enter();

        stateTime = enemy.stunDuration;
        rb.velocity = new Vector2( -enemy.facingDir * enemy.stunDistance.x,enemy.stunDistance.y );
        //enemy.SetVelocity( -enemy.facingDir * enemy.stunDistance.x,enemy.stunDistance.y );
        //不适用SetVelocity的原因是因为SetVelocity绑定了Flip判断,会翻转
    }
    public override void Update()
    {
        base.Update();

        if (stateTime < 0)
        {
            stateMachine.ChangeState(enemy.idleState);
        }
    }

    public override void Exit()
    {
        base.Exit();
    }

}

新加主功能二、被击中后人物会红白闪烁效果

在EntityFX.cs 新加方法 RedColorBlink()和CancleRedColorBlink()
在SkeletonStun状态开始调用闪烁开始(),结束调用闪烁结束()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 击中后特效脚本
public class EntityFX : MonoBehaviour
{
    private SpriteRenderer sr;

    [Header("FlashFX")]
    [SerializeField] private float flashDurantion;
    [SerializeField] private Material hitMat;  //新Matrial
    private Material originMat; //旧Matrial,用完改回去

    private void Start()
    {
        sr = GetComponentInChildren<SpriteRenderer>();
        originMat = sr.material;  //旧Matrial
    }
    private IEnumerator FlashFX()
    {
        sr.material = hitMat;
        yield return new WaitForSeconds(flashDurantion);
        sr.material = originMat;
    }
    private void RedColorBlink()
    {
        if(sr.color != Color.white)
            sr.color = Color.white;
        else
            sr.color = Color.red;
    }
    private void CancleRedColorBlink()
    {
    	//取消所有调用
		CancelInvoke();
        sr.color = Color.white;
    }
}

在骷髅怪,测试的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkeletonStunState : SkeletonState
{
    public SkeletonStunState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName, Enemy_Skeleton _enemy)
        : base(_enemyBase, _stateMachine, _animBoolName, _enemy)
    {
    }

    public override void Enter()
    {
        base.Enter();

        stateTime = enemy.stunDuration;
        rb.velocity = new Vector2( -enemy.facingDir * enemy.stunDistance.x,enemy.stunDistance.y );
        //不适用SetVelocity的原因是因为SetVelocity绑定了Flip判断,会翻转

        //开始body红白闪烁
        enemy.fx.InvokeRepeating("RedColorBlink", 0, 0.1f);
    }
    public override void Update()
    {
        base.Update();

        if (stateTime < 0)
        {
            stateMachine.ChangeState(enemy.idleState);
        }
    }

    public override void Exit()
    {
        base.Exit();
        //取消body红白闪烁
        enemy.fx.Invoke("CancleRedColorBlink", 0);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值