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

Question Stop Finding another target if there's a target present

Discussion in 'Scripting' started by Therapist, Jun 19, 2023.

  1. Therapist

    Therapist

    Joined:
    Dec 25, 2015
    Posts:
    3
    I'm working on a tower defense game and currently building the tower target functionality.
    Right now I find the closest enemy from the available enemies within the range and start shooting them.

    However it switches target enemy in case one enemy comes close than the current target.
    What I want is the once the target is found it should keep on shooting it and shouldn't switch the target.

    Attaching the scripts.

    Tower script

    private void Update()
    {
    if (Input.GetMouseButtonDown(0))
    {
    ShowTowerRange();
    }

    GetClosestEnemy();
    TurnTowardsEnemy();

    if (timeBetweenBullets < attackInterval)

    {
    timeBetweenBullets += Time.deltaTime;
    //print(attackInterval);

    if (timeBetweenBullets >= attackInterval)
    {
    Shoot();
    }
    }
    }

    Shoot functionality

    void Shoot()
    {
    GameObject bullet = Instantiate(towerProjectile, shootPoint.position, shootPoint.rotation);

    timeBetweenBullets = 0f;
    }

    Projectile functionality

    float speed = 5f;
    Tower tower;
    Transform target;

    // Start is called before the first frame update
    void Start()
    {
    tower = GameObject.FindGameObjectWithTag("Tower").GetComponent<Tower>();
    target = tower.GetClosestEnemy().transform;
    Destroy(gameObject, 2f);
    }

    private void Update()
    {
    transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
    //transform.forward * Time.deltaTime * speed;
    }
     

    Attached Files:

  2. Reedex

    Reedex

    Joined:
    Sep 20, 2016
    Posts:
    386
    ok, so what i would tweak is in the checkclosest function, assuming it's null from start. Once you get your closest target, you can only assign new target if target == null. you can set it to null once it gets out of range. p.s. try to use code tags. makes it much easier to read when posting code.)
     
    Last edited: Jun 19, 2023
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    Only skimmed over the unformatted code, but i generally agree with what was already said.
    1. If you got a target, check if it's still in range and alive. Otherwise set it to null.
    2. If you dont have a target, try to find a new one by whatever function. If you find one, set target.
    That's all there is to it. The above can also be implemented directly in that order. Dont make it exclusive tho, as you may find out your target died and want a new one the same frame.
     
    Therapist and Bunny83 like this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    FYI: Here's how to post code on the forums.
     
    Therapist likes this.
  5. Therapist

    Therapist

    Joined:
    Dec 25, 2015
    Posts:
    3
    Thanks , it worked!
     
  6. Therapist

    Therapist

    Joined:
    Dec 25, 2015
    Posts:
    3
    Thanks, it worked!