Hi, im new to unity. i was searching through some old forums to get the enemies to chase after the player. the problem is the positition of ‘y’ keeps changing too so the enemies floating above the air and keeps getting up. pls help (sorry for bad english)
this is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class zombieNavigation : MonoBehaviour
{
[Header(“Main Setting”)]
public GameObject player;
public GameObject toBeDestroyed;
public float speed = 1f;
public Transform target;
// Start is called before the first frame update
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
if (!player)
{
Debug.Log("ERROR could not find Player!");
}
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(player.transform.position, transform.position);
transform.LookAt(target);
if (distance < 60)
{
Debug.Log("player is close");
var delta = player.transform.position - transform.position;
delta.Normalize();
var moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);
}
else
{
Debug.Log("not close yet " + distance);
}
}
}