Y
Y
yraiv2022-01-22 12:57:44
Unity
yraiv, 2022-01-22 12:57:44

How to stop script execution while keeping the last turning point?

Game in 2d. When enemies enter the area, a script is triggered, during which my tower should look at the first enemy that entered, then if it disappears, then the program does not die, but moves to the next one, if it exists, and if not, it simply remains turned as it was before. Here is the code: The problem is that it looks at the last object, but I need it to look at the one that enters the zone first

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

public class TowersPistol : MonoBehaviour
{

    private Transform target;
    private CircleCollider2D CirCol;
    private bool YesOrNoZombieOnMap;
  
   



    public void FindPosZombie()
    {
        target = GameObject.FindGameObjectWithTag("Zombie").GetComponent<Transform>();
        if(target == null)
        {
            Вот что тут написать, чтоб если он не может найти объект с тегом нужным и взять таргет от него, то остановиться надо на этом месте должен
        }   
    }

    void OnTriggerStay2D(Collider2D collider)
    {
            if (collider.gameObject.tag == "Zombie")
            {
            FindPosZombie();
 transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, Mathf.Atan2(target.transform.position.y - transform.position.y, target.transform.position.x - transform.position.x) * Mathf.Rad2Deg + 90);            }

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Farawa, 2022-01-23
@yraiv

return;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question