How to trigger enemy when player apporach to enemy,enemy has to fire player?

Enemy when player apporaches to enemy,enemy has to fire.How can I do it?

Set a trigger or distance check

1 Like

I used but trigger couldn’t enable unfortunally.

Try again until you get it working.

1 Like
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Buyu : MonoBehaviour
{
    public Transform buyuNesnesi; //Büyü yapmamıza yarar.
    public Transform hedef;
  
    void atesle()
    {
        transform.LookAt(hedef); //Yönelim bu yöne doğru.
        Transform vur = Instantiate(buyuNesnesi, transform.position, Quaternion.identity) as Transform; //Tip uyuşmazlığı olmasın diye.
        vur.GetComponentInParent<Rigidbody2D>().velocity = Vector3.forward * 10f * Time.deltaTime;//Hangi hızla yollanacak.
        Quaternion gidisat = Quaternion.Lerp(transform.rotation, hedef.rotation, 10f + Time.deltaTime);
        vur.rotation = Quaternion.RotateTowards(transform.rotation, gidisat, 30f * Time.deltaTime);
        Destroy(vur.gameObject, 5f);
    }
    void OnTriggerEnter2D(Collider2D carpisan)
    {
        if(carpisan.gameObject.tag=="Player")
        {
            atesle();
        }
    }
}

This is my code. Where is my mistake you think?

Hard to tell just by looking at the code. What happens when you run this code? How is what happens different from what you want to happen or expect to happen?

My expection is when player apporaches to enemy,enemy has to has be triggered to fire to player.
However,This code couldn’t run this.
Just,Enemy fired to player twice.
But That has to be continous.

Ok and… what’s happening with this code?

I edited my comment.Do you see new comment?

As I said This script is supplying to that player fires twice.

If you want continuously firing then you need to change your logic…if player trigger than enemy can attack while enemy can attack just fire continuously till player dead or out of range …

I solved it.Thanks sir.