Hi, I’m quite new to unity (1 month) but I still know the basic features, component and some of the scripting.
The enemy doesn’t follow the player but goes to the middle and stop. Its not the rigidbody as when I changed it into kinematic it still wouldnt work. I have also changed the style of the code (by getting rid of the new Vector2 and replacing it with the Vector2.MoveTowards
transform.position = Vector2.MoveTowards(transform.position, player.transform.position, enemySpeed);
entirely instead of only using the x coordinates but still wouldnt work, it would do the same with a bit of jumping continously)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBehaviour : MonoBehaviour
{
// Cached References
[SerializeField] GameObject player;
[SerializeField] float enemySpeed = 5f;
Vector2 enemyTrackPosition;
private void Start()
{
enemySpeed *= Time.deltaTime;
}
private void Update()
{
enemyTrackPosition = Vector2.MoveTowards(transform.position, player.transform.position, enemySpeed);
transform.position = new Vector2(enemyTrackPosition.x, transform.position.y);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player")
{
Destroy(collision.gameObject);
}
}
}