Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy doesn't follow the player

$
0
0
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 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); } } }

Viewing all articles
Browse latest Browse all 1488


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>