I just started making a game. For whatever reason, the enemy continuously teleports to the origin for no reason. Here is my code so far;
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;
}
}
I have the target object assigned to the player. Someone please help because I can't find a solution anywhere.
CodePudding user response:
This is because transform.forward
is a result axis, not a specific location, and gives you numbers around -1 to 1. To fix that just try:
transform.position = transform.forward * speed * Time.deltaTime;