敌人的动作旋转应该攻击玩家,但不是吗?

时间:2019-02-15 06:29:11

标签: c# unity3d

基本敌人

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

public class BasicEnemy : MonoBehaviour
{
    public Transform target;
    public float speed = 3f;
    public float attack1Range = 1f;
    public int attack1Damage = 1;
    public float timeBetweenAttacks;


    // Use this for initialization
    void Start()
    {
        Rest();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void MoveToPlayer()
    {
        //rotate to look at player
        transform.LookAt(target.position);
        transform.Rotate(new Vector3(0, -90, 0), Space.Self);

        //move towards player
        if (Vector3.Distance(transform.position, target.position) > attack1Range)
        {
            transform.Translate(new Vector3(speed * Time.deltaTime, 0,     0));
        }
    }

    public void Rest()
    {

    }
}

敌人领土

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


public class EnemyTerritory : MonoBehaviour
{
    public BoxCollider territory;
    GameObject player;
    bool playerInTerritory;

    public GameObject enemy;
    BasicEnemy basicenemy;

    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        basicenemy = enemy.GetComponent<BasicEnemy>();
        playerInTerritory = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (playerInTerritory)
        {
            basicenemy.MoveToPlayer();
        }

        if (playerInTerritory)
        {
            basicenemy.Rest();
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == player)
        {
            playerInTerritory = true;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject == player)
        {
            playerInTerritory = false;
        }
    }
}

欢迎任何反馈,我会尝试使添加到游戏中的敌人朝着玩家的方向前进,就好像依附并跟随,一旦完成动作,我就会让他们向玩家射击。现在,我添加的所有新敌人都在快速旋转。我没有语法错误。

1 个答案:

答案 0 :(得分:1)

Update函数中肯定有一些问题,因为您要分配(=)而不是检查条件中的相等性(==)。

您不是要playerInTerritory == true吗?

void Update()
{
    if (playerInTerritory)
    {
        basicenemy.MoveToPlayer();
    } 
    else 
    {        
        basicenemy.Rest();
    }
}

如果这样做没有帮助,请在触发器的enter / exit函数中添加一些Debug.Log语句,以查看何时调用它们。


编辑

好的,我看着旋转。如果您想让敌人跟随玩家直到其在攻击范围之内,那么您的旋转方向和平移都是错误的。

旋转

// Rotate to look at player
transform.LookAt(target.position);
transform.Rotate(new Vector3(0, -90, 0), Space.Self);  

这是什么,它会使敌人转向玩家(您想要的),而第二行则使敌人旋转(每帧逆时针旋转90°)。如果您不希望敌人旋转,则不需要防线。 只需保留“查找”功能即可。

翻译

平移问题是您沿 X 轴的方向移动,而不是播放器的方向。您应该先计算方向矢量并沿其平移。

其要旨始终是:

// normalized, so the vector affects only direction, not speed
Vector3 direction = (FROM.position - TO.position).normalized;

所以,那里:

Vector3 enemyToTargetDirection = (target.position - transform.position).normalized;
enemyToTargetDirection.y = 0; // stay in the same height
transform.Translate(enemyToTargetDirection * speed * Time.deltaTime);

您可以这样做,但是我认为当敌人逐渐向您旋转时,它看起来会更好,而不仅仅是“捕捉”您的方向。 在这种情况下,结果函数将如下所示:

public void MoveToPlayer()
{
    Vector3 upwardsAxis = Vector3.up;

    float distanceToPlayer = Vector3.Distance(transform.position, target.position);
    if (distanceToPlayer > attack1Range)
    {
        float step = Time.deltaTime * speed;
        Vector3 lookDirection = target.position - transform.position;
        if (lookDirection != Vector3.zero) // Prevents rotation errors
        {
            // Rotate over time
            Quaternion lookRotation = Quaternion.LookRotation(lookDirection, upwardsAxis);
            transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, step * 2);
        }
        transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    }
}

哦,顺便说一句,对于一般的运动/旋转/物理,您应该使用FixedUpdate()而不是常规的Update()(这会使运动更流畅),并且不要保持如果它们为空,请“唤醒”,“开始”,“更新”方法,因为它们会(稍微)影响性能。