- Home /
Question by
claudiumitu69 · Dec 06, 2019 at 12:44 PM ·
2denemyrpgsidescroller
enemy follow player 2d sidescroller rpg
my enemy is very confused, he s running away from the player when the game starts and if i go near him he s trying to go to the player then he s running and it s very confused what can i do, here is the script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : MonoBehaviour {
public int health;
public float speed;
public GameObject BloodEffect;
public bool FindPlayer;
public Animator enemyAnim;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (health <= 0)
{
Destroy(gameObject);
}
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
public void TakeDamage(int damage)
{
Instantiate(BloodEffect, transform.position, Quaternion.identity);
health -= damage;
Debug.Log("Damage Taken");
}
void GiveDamage()
{
if (FindPlayer)
{
if (GameObject.FindGameObjectWithTag("Player"))
{
GetComponent<Health>().Damaged();
}
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.transform.name == "Player")//or tag
{
collision.GetComponent<Health>().Damaged();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Finding Positions of other game objects and using them as an argument for an if statement. 0 Answers
Enemies Shoot Towards Face, Would Like to have them Shoot to Chest 1 Answer
What is wrong about this script / why cant the player walk up and down? 1 Answer
Why is my gameObject falling so slow? 1 Answer
3d Buzz 2D tutorial - CharacterController2D - Enemies. 1 Answer