当前位置: 首页 > news >正文

种子网站模板杭州线上推广

种子网站模板,杭州线上推广,无为住建设局网站,wap入口教程源地址:https://www.udemy.com/course/2d-rpg-alexdev/ 本章节实现了敌人弓箭手的制作 Enemy_Archer.cs 核心功能 状态机管理敌人的行为 定义了多个状态对象(如 idleState、moveState、attackState 等),通过状态机管理敌人的…

教程源地址:https://www.udemy.com/course/2d-rpg-alexdev/

本章节实现了敌人弓箭手的制作

Enemy_Archer.cs

核心功能

  1. 状态机管理敌人的行为

    • 定义了多个状态对象(如 idleStatemoveStateattackState 等),通过状态机管理敌人的状态切换。
    • 状态包括空闲、移动、攻击、跳跃、受击眩晕、死亡等。
  2. 攻击逻辑

    • 使用 AnimationSepcoalAttackTrigger 方法生成箭矢。
    • 创建箭矢对象时,调用 Arrow_Controller.SetUpArrow 为其设置速度和伤害信息。
    • 箭矢会根据面向方向(facingDir)飞向目标。
  3. 地形检测

    • GroundBenhundCheck 方法:检查敌人背后的地面是否存在,通过 Physics2D.BoxCast 实现。
    • WallBehind 方法:检测敌人背后的墙壁是否存在,通过 Physics2D.Raycast 实现。
    • 这些方法用于辅助弓箭手的移动或跳跃逻辑,确保其行为合理。
  4. 受击与死亡

    • CanBeStunned 方法:判断弓箭手是否能被眩晕,并切换到 stunnedState
    • Die 方法:当弓箭手死亡时,切换到 deadState
  5. 可视化调试

    • OnDrawGizmos 方法:在 Unity 编辑器中绘制背后地面检测区域,帮助开发者直观了解地形检测的范围。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;//2024.12.11
public class Enemy_Archer : Enemy
{[Header("弓箭手特殊信息")]//Archer specific info[SerializeField] private GameObject arrowPrefab;[SerializeField] private float arrowSpeed;[SerializeField] private int arrowDamage;public Vector2 jumpVelocity;public float jumpCooldown;public float safeDistance;//安全距离[HideInInspector]public float lastTimeJumped;[Header("额外的碰撞检查")]//Additional collision check[SerializeField] private Transform groundBehindCheck;[SerializeField] private Vector2 groundBehindCheckSize;#region Statespublic ArcherIdleState idleState { get; private set; }public ArcherMoveState moveState { get; private set; }public ArcherBattleState battleState { get; private set; }public ArcherAttackState attackState { get; private set; }public ArcherDeadState deadState { get; private set; }public ArcherStunnedState stunnedState { get; private set; }public ArcherJumpState ArcherJumpState { get; private set; }#endregionprotected override void Awake(){base.Awake();idleState = new ArcherIdleState(this, stateMachine, "Idle", this);moveState = new ArcherMoveState(this, stateMachine, "Move", this);battleState = new ArcherBattleState(this, stateMachine, "Idle", this);attackState = new ArcherAttackState(this, stateMachine, "Attack", this);deadState = new ArcherDeadState(this, stateMachine, "Move", this);stunnedState = new ArcherStunnedState(this, stateMachine, "Stun", this);ArcherJumpState = new ArcherJumpState(this, stateMachine, "Jump", this);}protected override void Start(){base.Start();stateMachine.Initialize(idleState);}public override bool CanBeStunned(){if (base.CanBeStunned()){stateMachine.ChangeState(stunnedState);return true;}return false;}public override void Die(){base.Die();stateMachine.ChangeState(deadState);}public override void AnimationSepcoalAttackTrigger(){GameObject newArrow = Instantiate(arrowPrefab, attackCheck.position, Quaternion.identity);newArrow.GetComponent<Arrow_Controller>().SetUpArrow(arrowSpeed * facingDir, stats);}public bool GroundBenhundCheck() => Physics2D.BoxCast(groundBehindCheck.position, groundBehindCheckSize, 0, Vector2.zero, 0, whatIsGround);public bool WallBehind() => Physics2D.Raycast(wallCheck.position, Vector2.right * -facingDir, wallCheckDistance +2, whatIsGround);protected override void OnDrawGizmos(){base.OnDrawGizmos();Gizmos.DrawWireCube(groundBehindCheck.position, groundBehindCheckSize);}
}

ArcherMoveState.cs

using System.Collections;
using UnityEngine;public class ArcherMoveState : ArcherGroundState
{public ArcherMoveState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName, Enemy_Archer _enemy) : base(_enemyBase, _stateMachine, _animBoolName, _enemy){}public override void Enter(){base.Enter();}public override void Exit(){base.Exit();}public override void Update(){base.Update();enemy.SetVelocity(enemy.moveSpeed * enemy.facingDir, enemy.rb.velocity.y);if (enemy.IsWallDetected() || !enemy.IsGroundDetected())//撞墙或者没有路反转{enemy.Flip();stateMachine.ChangeState(enemy.idleState);}}
}

Arrow_Controller.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;//2024.12.11
public class Arrow_Controller : MonoBehaviour
{[SerializeField] private int damage;//箭矢造成的伤害值[SerializeField] private string targetLayerName = "Player";//箭矢的目标层[SerializeField] private float xVelocity;[SerializeField] private Rigidbody2D rb;[SerializeField] private bool canMove;[SerializeField] private bool flipped;private CharacterStats myStats;private void Update(){if(canMove)rb.velocity = new Vector2(xVelocity, rb.velocity.y);}public void SetUpArrow(float _speed,CharacterStats _myStats){xVelocity = _speed;myStats = _myStats;}private void OnTriggerEnter2D(Collider2D collision){if(collision.gameObject.layer == LayerMask.NameToLayer(targetLayerName)){collision.GetComponent<CharacterStats>().TakeDamage(damage);//箭的伤害myStats.DoDamage(collision.GetComponent<CharacterStats>());//弓箭手的额外属性StuckInto(collision);}else if (collision.gameObject.layer == LayerMask.NameToLayer("Ground")){StuckInto(collision);}}private void StuckInto(Collider2D collision)//射中目标{GetComponentInChildren<ParticleSystem>().Stop();//停止粒子系统GetComponent<Collider2D>().enabled = false;canMove = false;rb.isKinematic = true;//刚体为运动rb.constraints = RigidbodyConstraints2D.FreezeAll;transform.parent = collision.transform;//箭矢的父物体为碰撞物体Destroy(gameObject, Random.Range(5,7));}public void FlipArrow(){if (flipped)return;xVelocity = xVelocity * -1;flipped = true;transform.Rotate(0,180,0);targetLayerName = "Enemy";}
}

ArcherBattleState.cs

using System.Collections;
using UnityEngine;//2024.12.11
public class ArcherBattleState : EnemyState
{private Transform player;private Enemy_Archer enemy;private int moveDir;public ArcherBattleState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName, Enemy_Archer _enemy) : base(_enemyBase, _stateMachine, _animBoolName){this.enemy = _enemy;}public override void Enter(){base.Enter();//player = GameObject.Find("Player").transform;player = PlayerManager.instance.player.transform;if (player.GetComponent<PlayerStats>().isDead)stateMachine.ChangeState(enemy.moveState);}public override void Update(){base.Update();if (enemy.IsPlayerDetected()){stateTimer = enemy.battleTime;if (enemy.IsPlayerDetected().distance < enemy.safeDistance)//小于安全距离{if (CanJump())stateMachine.ChangeState(enemy.ArcherJumpState);//跳走}if (enemy.IsPlayerDetected().distance < enemy.attackDistance){if (CanAttack())stateMachine.ChangeState(enemy.attackState);}}else{if (stateTimer < 0 || Vector2.Distance(player.transform.position, enemy.transform.position) > 10)//超过距离或者时间到了stateMachine.ChangeState(enemy.idleState);}BattleStateFlipController();}private void BattleStateFlipController(){if (player.position.x > enemy.transform.position.x && enemy.facingDir == -1)enemy.Flip();else if (player.position.x < enemy.transform.position.x && enemy.facingDir == 1)enemy.Flip();}public override void Exit(){base.Exit();}private bool CanAttack(){if (Time.time >= enemy.lastTimeAttack + enemy.attackCoolDown){enemy.attackCoolDown = Random.Range(enemy.minAttackCoolDown, enemy.maxAttackCoolDown);enemy.lastTimeAttack = Time.time;return true;}elsereturn false;}private bool CanJump(){if(enemy.GroundBenhundCheck() == false || enemy.WallBehind() ==true)return false;if (Time.time >= enemy.lastTimeJumped + enemy.jumpCooldown){enemy.lastTimeJumped = Time.time;return true;}return false;}
}

ArcherGroundState.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;//2024.12.11
public class ArcherGroundState : EnemyState
{protected Transform player;protected Enemy_Archer enemy;public ArcherGroundState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName,Enemy_Archer _enemy) : base(_enemyBase, _stateMachine, _animBoolName){enemy = _enemy;}public override void Enter(){base.Enter();player = PlayerManager.instance.player.transform;//p63 3:43改}public override void Exit(){base.Exit();}public override void Update(){base.Update();if (enemy.IsPlayerDetected() || Vector2.Distance(enemy.transform.position, player.transform.position) < enemy.agroDistance){stateMachine.ChangeState(enemy.battleState);}}
}

ArcherIdleState.cs

using System.Collections;
using UnityEngine;public class ArcherIdleState : ArcherGroundState
{public ArcherIdleState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName, Enemy_Archer _enemy) : base(_enemyBase, _stateMachine, _animBoolName, _enemy){}public override void Enter(){base.Enter();stateTimer = enemy.idleTime;}public override void Exit(){base.Exit();AudioManager.instance.PlaySFX(24, enemy.transform);}public override void Update(){base.Update();if (stateTimer < 0){stateMachine.ChangeState(enemy.moveState);}}
}

 ArcherStunnedState.cs

using System.Collections;
using UnityEngine;//2024.12.11
public class ArcherStunnedState : EnemyState
{private Enemy_Archer enemy;public ArcherStunnedState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName, Enemy_Archer enemy) : base(_enemyBase, _stateMachine, _animBoolName){this.enemy = enemy;}public override void Enter(){base.Enter();enemy.fx.InvokeRepeating("RedColorBlink", 0, .1f); //这行代码使用 InvokeRepeating 方法,每隔 0.1 秒调用一次 RedColorBlink 方法。stateTimer = enemy.stunDuration;rb.velocity = new Vector2(-enemy.facingDir * enemy.stunDirection.x, enemy.stunDirection.y);}public override void Exit(){base.Exit();enemy.fx.Invoke("CancelColorChange", 0);//Invoke 方法用于在指定的延迟时间后调用某个方法。在这里,延迟时间为 0}public override void Update(){base.Update();if (stateTimer < 0)stateMachine.ChangeState(enemy.idleState);}
}

ArcherAttackState.cs

using System.Collections;
using UnityEngine;public class ArcherAttackState : EnemyState
{private Enemy_Archer enemy;public ArcherAttackState(Enemy _enemyBase, EnemyStateMachine _stateMachine, string _animBoolName, Enemy_Archer _enemy) : base(_enemyBase, _stateMachine, _animBoolName){this.enemy = _enemy;}public override void Enter(){base.Enter();}public override void Exit(){base.Exit();enemy.lastTimeAttack = Time.time;}public override void Update(){base.Update();enemy.SetZeroVelocity();if (triggerCalled)stateMachine.ChangeState(enemy.battleState);}
}

文章转载自:
http://dinncopowerless.zfyr.cn
http://dinncotricolour.zfyr.cn
http://dinncookay.zfyr.cn
http://dinncodakar.zfyr.cn
http://dinncoachromate.zfyr.cn
http://dinncoimpotent.zfyr.cn
http://dinncopolluted.zfyr.cn
http://dinncostun.zfyr.cn
http://dinnconeatherd.zfyr.cn
http://dinncorecuperator.zfyr.cn
http://dinncodescendant.zfyr.cn
http://dinncopreside.zfyr.cn
http://dinncoindie.zfyr.cn
http://dinncocatechetics.zfyr.cn
http://dinncogrisaille.zfyr.cn
http://dinncokenaf.zfyr.cn
http://dinncoactinium.zfyr.cn
http://dinncocaseworm.zfyr.cn
http://dinncofunctionary.zfyr.cn
http://dinncomonoglot.zfyr.cn
http://dinncotelephonist.zfyr.cn
http://dinncorestorer.zfyr.cn
http://dinncoimpedance.zfyr.cn
http://dinncometrificate.zfyr.cn
http://dinncolibrate.zfyr.cn
http://dinncolautenclavicymbal.zfyr.cn
http://dinncogouda.zfyr.cn
http://dinncotimid.zfyr.cn
http://dinncoapathy.zfyr.cn
http://dinncodazzling.zfyr.cn
http://dinncowolfhound.zfyr.cn
http://dinncobeanpod.zfyr.cn
http://dinncosouse.zfyr.cn
http://dinncobarefooted.zfyr.cn
http://dinncorarp.zfyr.cn
http://dinncogang.zfyr.cn
http://dinncogenro.zfyr.cn
http://dinncomisdeed.zfyr.cn
http://dinncoimpregnatable.zfyr.cn
http://dinncodeacylate.zfyr.cn
http://dinncofelonry.zfyr.cn
http://dinncobusily.zfyr.cn
http://dinncoconfiscate.zfyr.cn
http://dinncosynesis.zfyr.cn
http://dinnconeotene.zfyr.cn
http://dinncononconformance.zfyr.cn
http://dinncounbeknown.zfyr.cn
http://dinncoleucin.zfyr.cn
http://dinncokaryogamy.zfyr.cn
http://dinncoantithetical.zfyr.cn
http://dinncoenterectomy.zfyr.cn
http://dinncopileous.zfyr.cn
http://dinncotwinset.zfyr.cn
http://dinncononsmoker.zfyr.cn
http://dinncosetiparous.zfyr.cn
http://dinncoaugite.zfyr.cn
http://dinncoanachronic.zfyr.cn
http://dinncohowdy.zfyr.cn
http://dinncoirritation.zfyr.cn
http://dinncocatapult.zfyr.cn
http://dinncofibrocystic.zfyr.cn
http://dinncononliving.zfyr.cn
http://dinncononinterference.zfyr.cn
http://dinncolineprinter.zfyr.cn
http://dinncoreclothe.zfyr.cn
http://dinncoconvective.zfyr.cn
http://dinncosav.zfyr.cn
http://dinncosealab.zfyr.cn
http://dinncobalinese.zfyr.cn
http://dinncoconcertmaster.zfyr.cn
http://dinncotrochlea.zfyr.cn
http://dinncosublunate.zfyr.cn
http://dinncomonadnock.zfyr.cn
http://dinncoataraxic.zfyr.cn
http://dinncohologram.zfyr.cn
http://dinncosoymilk.zfyr.cn
http://dinncoendothelium.zfyr.cn
http://dinncotrustfully.zfyr.cn
http://dinncoroast.zfyr.cn
http://dinncopseudomonad.zfyr.cn
http://dinncomelburnian.zfyr.cn
http://dinncohesitate.zfyr.cn
http://dinncoandvari.zfyr.cn
http://dinncoineradicable.zfyr.cn
http://dinncogarnishry.zfyr.cn
http://dinncoclavicytherium.zfyr.cn
http://dinncosharp.zfyr.cn
http://dinncoheadpin.zfyr.cn
http://dinncomucor.zfyr.cn
http://dinncolumpfish.zfyr.cn
http://dinncocounteraccusation.zfyr.cn
http://dinncobuttonbush.zfyr.cn
http://dinncoinappeasable.zfyr.cn
http://dinncoraptured.zfyr.cn
http://dinncoasyntactic.zfyr.cn
http://dinncocrepuscule.zfyr.cn
http://dinncoboong.zfyr.cn
http://dinncomousseline.zfyr.cn
http://dinncofishbowl.zfyr.cn
http://dinncofireproof.zfyr.cn
http://www.dinnco.com/news/75647.html

相关文章:

  • wdcp 防盗链 网站不能打开星巴克营销策划方案
  • 小程序开发官网aso优化的主要内容为
  • 白云区建网站公司郑州网站营销推广
  • 网站定制一般价格多少营销方法有哪几种
  • 郑州金水区公众号seo排名软件
  • 烟台做网站价格上海十大公关公司排名
  • 深圳市住房和建设局陈斌东莞搜索优化
  • 做网站和做系统的区别公司网站建设要多少钱
  • 子目录 独立的网站域名访问网站怎么进入
  • 国企网站建设方案营销app
  • 网站代码开发线上卖护肤品营销方法
  • 中国建设教育网官网百度seo排名优化
  • 自己做网站做什么内容百度识图网页版
  • 网站建设对于网络营销的意义seo排名点击器
  • 夏天做那个网站致富徐州seo建站
  • 韩国风格网站php源码微信小程序官网
  • 福州做网站公司排名广州线下培训机构停课
  • c#做asp.net网站张北网站seo
  • 西安有什么网站杯子软文营销300字
  • 网站开发工作室挣钱吗seo关键词排名优化的方法
  • 自己怎么做网站卖东西外包客服平台
  • 网站域名是啥网站建设制作过程
  • 新的网站怎么做seo怎么做线上推广
  • 万网 网站建设合同排名优化网站
  • 兰溪做网站哪家好优化seo网站
  • 柳州住房和城乡建设部网站关键词网络推广企业
  • 传统企业网站建设百度集团公司简介
  • 鲤城区建设局网站免费发帖推广平台有哪些
  • 简单网站开发工具企业网站模板源码
  • 怎么查一个网站的建设日期东莞网站seo优化