Unity3D-创建敌人攻击逻辑

敌人攻击逻辑脚本

public enum Type { Melee, Range, Boss }

public Type enemyType;

public float enemyone = 1;

public Transform target;

public BoxCollider meleeArea;

public GameObject bullet;

protected Rigidbody _rigidbody;

protected NavMeshAgent _navMeshAgent;

protected Animator _animator;

protected Health _health;

private float _targetRadius;

private float _targetRange;

private bool _isChase;

private bool _isAttack;

private void Awake()

{

    _rigidbody = GetComponent<Rigidbody>();

    _navMeshAgent = GetComponent<NavMeshAgent>();

    _animator = GetComponentInChildren<Animator>();

    _health = GetComponent<Health>();

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以通过以下步骤在Unity中实现敌人在一定范围内随机行走: 1. 创建一个空对象,并将其命名为“Enemy”。 2. 在“Enemy”对象下创建一个子对象,并将其命名为“Vision”。 3. 在“Vision”对象下添加一个“Sphere Collider”组件,并将其半径设置为敌人的视野范围。 4. 在“Vision”对象下添加一个“Trigger”组件,并将其设置为“Is Trigger”。 5. 在“Enemy”对象下添加一个“Nav Mesh Agent”组件。 6. 创建一个新的脚本,并将其附加到“Enemy”对象上。 7. 在脚本中定义一个变量来存储敌人的目标位置。 8. 在“Update”函数中,检测敌人是否在视野范围内。如果是,则设置目标位置为一个随机位置,并使用“Nav Mesh Agent”组件来移动敌人到该位置。如果不是,则停止移动。 以下是代码示例: ``` using UnityEngine; using UnityEngine.AI; public class EnemyController : MonoBehaviour { public float visionRange = 10f; // 视野范围 public float walkSpeed = 3.5f; // 行走速度 private NavMeshAgent agent; private Vector3 targetPosition; void Start() { agent = GetComponent<NavMeshAgent>(); agent.speed = walkSpeed; } void Update() { if (IsPlayerInSight()) { MoveToRandomPosition(); } else { StopMoving(); } } bool IsPlayerInSight() { Collider[] colliders = Physics.OverlapSphere(transform.position, visionRange); foreach (Collider collider in colliders) { if (collider.CompareTag("Player")) { return true; } } return false; } void MoveToRandomPosition() { if (agent.remainingDistance < 1f) // 到达目标位置后再设置新的目标位置 { float x = Random.Range(-10f, 10f); float z = Random.Range(-10f, 10f); targetPosition = transform.position + new Vector3(x, 0f, z); agent.SetDestination(targetPosition); } } void StopMoving() { agent.ResetPath(); } } ``` 请注意,此示例中的代码仅处理敌人的移动逻辑。如果您需要敌人攻击玩家或与玩家交互等其他行为,请根据需要添加相应的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值