Currently I have the enemy fully locked on the player, but I'm trying to make the enemy character move towards the player's x-axis coordinates while forever moving down the y-axis.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float moveSpeed;
public Transform target;
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent();
}
// Update is called once per frame
void Update()
{
if(Vector2.Distance(transform.position, target.position) > 0.5)
{
transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Destroy"))
{
Destroy(gameObject);
}
}
}
↧