Advertisement

I need some help on this bug i have

Started by July 05, 2020 10:12 PM
0 comments, last by _Yeetus25 4 years, 4 months ago

Ok so the problem is with the enemies. I have set a radius on them and when you go into their radius they start moving towards you and shooting you. But let's say I have to of those enemies, and when I get into the radius of one, it counts I have also moved into the other one's radius. So if I go into the radius of one enemy. He starts shooting me and moving towards me and then the other enemy does it as well. If you don't understand then I can try to make it more clear.

this is the code I have for the enemies. Help would be REALLY Appreciated since I'm 80% sure I won't know how to solve this problem myself.

EDIT: The second enemy only starts shooting bullets, he doesn't move towards the player

public float speed;
	public float stoppingDistance;
	public float retreatDistance;

	public float _radius = 10f;
	public static float radius;

	private float timeBtwShots;
	public float startTimeBtwShots;

	public GameObject projectile;
	private Transform player;

	NavMeshAgent agent;



	void Start()
	{
		player = GameObject.FindGameObjectWithTag("Player").transform;

		timeBtwShots = startTimeBtwShots;

		agent = GetComponent<NavMeshAgent>();
		radius = _radius;
	}


	void Update()
	{
		float distance = Vector2.Distance(player.position, transform.position);


		if (distance <= radius)
		{
			agent.SetDestination(player.position);
		}


		if (Vector2.Distance(transform.position, player.position) > stoppingDistance &amp;&amp; distance <= radius)
        {
            transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
			Debug.Log("Forward");
		}

        else if (Vector2.Distance(transform.position, player.position) < stoppingDistance &amp;&amp; Vector2.Distance(transform.position, player.position) > retreatDistance)
        {
            transform.position = this.transform.position;
        }

        else if (Vector2.Distance(transform.position, player.position) < retreatDistance)
        {
            transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
		}


        if (timeBtwShots <= 0)
        {
            Instantiate(projectile, transform.position, Quaternion.identity);
            timeBtwShots = startTimeBtwShots;
        }

        else
        {
            timeBtwShots -= Time.deltaTime;
        }
    }

This topic is closed to new replies.

Advertisement