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

Unity 2D Platformer Enemy follow Player on X-axis only, c#

$
0
0
Hey guys, I am making a battle system for my RPG game, and I've run into a problem. I want it so if the enemy chases the player non-stop *only* on the x-axis. Then, when the enemy gets to a certain distance from the player, it will start attacking. Here is a script I have: using System.Collections; using System.Collections.Generic; using UnityEngine; public class FollowPlayer : MonoBehaviour { private bool checkTrigger; public float speed; public Transform target; void Start () { target = GameObject.FindGameObjectWithTag("Player").GetComponent (); } // Update is called once per frame void Update () { if (checkTrigger) { transform.position = Vector2.MoveTowards (transform.position, target.position, speed * Time.deltaTime); } } void OnTriggerEnter2D(Collider2D other) { if (other.name == "Player") { checkTrigger = true; } } void OnTriggerExit2D(Collider2D other){ if (other.name == "Player") { checkTrigger = false; } } } The problem is this allows the enemy to go *any* direction, x-axis **and** y-axis, but I only want the enemy to go on the *x-axis* only. I would really appreciate help from anyone. :)

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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