敌人运动旋转应该是攻击球员而不是这样做?

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

BasicEnemy

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;
        }
    }
}

任何反馈都是受欢迎的我试图让我加入到游戏中的敌人移动到球员身上,好像要附加并跟随一旦我能完成这项运动,我会让他们在球员身上射门。现在我添加的所有新敌人都在快速旋转。我没有语法错误。

c# unity3d
1个回答
1
投票

更新函数肯定有些可疑,因为你在这个条件中分配(=)而不是检查相等性(==)。

你不是说playerInTerritory == true

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

如果这没有帮助,请在触发器输入/退出函数中添加一些Debug.Log语句,以查看它们何时被调用。


编辑

好吧,我看着旋转着。如果你想让敌人跟随玩家直到它在攻击范围内,你就会有错误的旋转和错误的翻译。

Rotation

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

这样做,它使敌人转向玩家(你想要的),第二行让敌人旋转(每帧逆时针旋转90°)。如果你不想让敌人旋转,你就不需要这条线。只需保持'Look At'功能。

Translation

翻译问题是你沿着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()(它使运动更流畅)并且不要保持'清醒','开始','更新'它们是空白的方法,因为它会影响性能(略)。

© www.soinside.com 2019 - 2024. All rights reserved.