Unity attach script to enemy object, instead my player affected

I am trying to enemy fire at me. I have a script and I attached script to enemy object. However, unexpectedly my player object fire non stop instead of enemy.I dont know how my player object affected.Also distance is not calculated properly. it shows 1000, then 1200 then 800 etc.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class shten : MonoBehaviour
{
    public GameObject AntagonisticElement;
    public GameObject Target;
    public float bulletSpeed;
    public float enemySpeed;
    public float bulletDestroyTime;
    public GameObject explsn;
    public GameObject bulletPrefab;
    public Transform bulletSpawn;

    Vector3 pos;
    public float dist;
    public float min = 200;
    public float max = 150;

    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        dist = Vector3.Distance(AntagonisticElement.transform.position, Target.transform.position);
        transform.LookAt(Target.transform.position);

        if (dist >= min)
        {
            AntagonisticElement.transform.position += AntagonisticElement.transform.forward * 4 * Time.deltaTime;
        }

        if (dist <= max)
        {
            shootAt();
        }
    }

    void shootAt()
    {
        Instantiate(explsn, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
        var bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * bulletSpeed;

        Destroy(bullet, bulletDestroyTime);
    }
}
728x90

0 Answers Unity attach script to enemy object, instead my player affected