为什么我的敌人总是统一传送到原点?

Why does my enemy keep teleporting to the origin in unity?

我刚开始制作游戏。无论出于何种原因,敌人都会无缘无故地不断传送到原点。到目前为止,这是我的代码;

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

public class Enemy : MonoBehaviour
{
    public float speed = 0.5f;
    public Transform target;

    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.LookAt(target);
        transform.position = transform.forward * speed * Time.deltaTime;
    }
}

我已将目标对象分配给玩家。请有人帮忙,因为我无法在任何地方找到解决方案。

这是因为 transform.forward 是一个结果轴,而不是一个特定的位置,并且给你的数字在 -1 到 1 之间。要解决这个问题,只需尝试:

transform.position += transform.forward * speed * Time.deltaTime;