可以让敌人追击并击中我,但是当他击中我时,他会消失在玩家的另一侧

问题描述 投票:0回答:1

我设置了调试日志,以查看是否未调用某些内容。所有日志都显示在控制台中。敌人可以击中玩家,但是当他这样做时,敌人会出现在玩家的另一侧。

https://www.youtube.com/watch?v=0McNnxUVM4o&feature=youtu.be是正在播放的视频。

我正试图让敌人跑向玩家,停下来,击中玩家,并停留在玩家的一侧,而不是另一侧。我与敌人联系在一起的唯一脚本是HP脚本。将其设置在OnTriggerEvent上会更好吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(Rigidbody))]
public class EnemyController : MonoBehaviour
{
    public float movementSpeed;
    public GameObject Target;
    public float lookRadius = 10f;
    // Attack
    public int AttackDamageMin;
    public int AttackDamageMax;
    public float AttackCooldownTimeMain;
    public float AttackCooldownTime;
    public float minAttackDistance = 10f;

    Transform target;
    NavMeshAgent agent;

    void Start()
    {
        target = PlayerManager.instance.player.transform;
        agent = GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        float distance = Vector3.Distance(target.position, transform.position);
        Debug.Log("Float Dist");

        if(distance < minAttackDistance)
        {
            StopEnemy();
            Debug.Log("Enemy Stop");

            AttackTarget();
            Debug.Log("Player is Attacked");
            agent.SetDestination(target.position);

            if (distance <= agent.stoppingDistance)
            {
                Facetarget();
                Debug.Log("Enemy is now Facing Player");
            }
            transform.Translate(Vector3.forward * movementSpeed);
        }
        else
        {
            GoToTarget();
            Debug.Log("Enemy is now going to Player");

            if (AttackCooldownTime > 0)
            {
                AttackCooldownTime -= Time.deltaTime;
                Debug.Log("Attack is now on Cooldown");
            }
            else
            {
                AttackCooldownTime = AttackCooldownTimeMain;
            }
        }
    }

    void AttackTarget()
    {
        Target.transform.GetComponent<Player>().TakeDamage(Random.Range(AttackDamageMin, AttackDamageMax));
        Debug.Log("Attacking");
    }

    void GoToTarget()
    {
        agent.isStopped = false;
        agent.SetDestination(target.transform.position);
        Debug.Log("Gototarget");
    }

    void StopEnemy()
    {
        agent.isStopped = true;
        Debug.Log("Stop");
    }

    void Facetarget()
    {
        Vector3 direction = (target.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, lookRadius);
    }
}
c# unity3d game-physics
1个回答
0
投票

此行

transform.Translate(Vector3.forward * movementSpeed);

似乎是问题所在,您正在使敌人前进很多。这在每帧都发生,并且没有按增量时间缩放。我不确定您是否打算将您的敌人向前推进,但是如果您是您,则应该按增量时间进行缩放]

 transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime);
© www.soinside.com 2019 - 2024. All rights reserved.