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

How do I make the enemy lock onto the player's x-axis coordinates?

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

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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