Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by huseyinbaba58, Aug 13, 2020.

  1. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    Enemy when player apporaches to enemy,enemy has to fire.How can I do it?
     
  2. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    Set a trigger or distance check
     
    huseyinbaba58 likes this.
  3. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    I used but trigger couldn't enable unfortunally.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Try again until you get it working.
     
    Yanne065 likes this.
  5. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Buyu : MonoBehaviour
    6. {
    7.     public Transform buyuNesnesi; //Büyü yapmamıza yarar.
    8.     public Transform hedef;
    9.  
    10.     void atesle()
    11.     {
    12.         transform.LookAt(hedef); //Yönelim bu yöne doğru.
    13.         Transform vur = Instantiate(buyuNesnesi, transform.position, Quaternion.identity) as Transform; //Tip uyuşmazlığı olmasın diye.
    14.         vur.GetComponentInParent<Rigidbody2D>().velocity = Vector3.forward * 10f * Time.deltaTime;//Hangi hızla yollanacak.
    15.         Quaternion gidisat = Quaternion.Lerp(transform.rotation, hedef.rotation, 10f + Time.deltaTime);
    16.         vur.rotation = Quaternion.RotateTowards(transform.rotation, gidisat, 30f * Time.deltaTime);
    17.         Destroy(vur.gameObject, 5f);
    18.     }
    19.     void OnTriggerEnter2D(Collider2D carpisan)
    20.     {
    21.         if(carpisan.gameObject.tag=="Player")
    22.         {
    23.             atesle();
    24.         }
    25.     }
    26. }
    27.  
    This is my code. Where is my mistake you think?
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    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?
     
  7. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    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.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Ok and... what's happening with this code?
     
  9. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    I edited my comment.Do you see new comment?
     
  10. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    As I said This script is supplying to that player fires twice.
     
  11. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    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 ..
     
  12. huseyinbaba58

    huseyinbaba58

    Joined:
    Feb 12, 2020
    Posts:
    146
    I solved it.Thanks sir.