Search Unity

Missile Lock - On goes haywire...

Discussion in 'Scripting' started by Divis10nStudios, Feb 4, 2020.

  1. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    I have this simple missile lock on script which i attach to the objects i want to be targets.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyLockController : MonoBehaviour
    6. {
    7.     bool LockCheck = false;
    8.     public Transform objectA ;
    9.     public Transform objectB ;
    10.     float dist;
    11.     public float lockDist;
    12.     public float lockTime;
    13.     public GuidedMissileController msl;
    14.  
    15.     public GameObject lockTone;
    16.  
    17.     void Update()
    18.     {
    19.        dist = Vector3.Distance( objectA.position, objectB.position);
    20.        if (LockCheck)
    21.        {
    22.           if(dist < lockDist)
    23.           {
    24.             StartCoroutine(MissileLock());
    25.           }
    26.        }
    27.  
    28.        else
    29.        {
    30.            msl.Target = null;
    31.  
    32.            lockTone.SetActive(false);
    33.        }
    34.     }
    35.  
    36.     void OnBecameVisible()
    37.     {
    38.         LockCheck = true;
    39.     }
    40.  
    41.     void OnBecameInvisible()
    42.     {
    43.         LockCheck = false;
    44.     }
    45.  
    46.     IEnumerator MissileLock()
    47.     {
    48.         yield return new WaitForSeconds(lockTime);
    49.         msl.Target = this.transform;
    50.  
    51.         lockTone.SetActive(true);
    52.         Debug.Log("Locked!");
    53.     }
    54. }
    55.  
    It works fine with a single target, but as soon as i add more than one into the scene, no matter how far they are from each other it doesn't work. Can someone help me with this?
    Also i already have a good missile script it's just the locking part.
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    Not sure what is object A and B and how whole thing works, but I would say that LockCheck possibly becomes false and reset the target, maybe you call OnBecameInvisible multiple times per frame?
    Btw move line 19 to 21 as you don't use dist every frame.
     
  3. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    Object A is target(script which object is on)
    Object B is player
    dist is the distance between them which I guess needs to be constantly checked?
    how do I limit OnBecameInvisible being called?
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    How does above script work with multiple targets? Where do you invoke OnbecameVisible/OnbecameInvisible?

    A couple of notes:
    • every time lockcheck is true and dist<lockdist you start the coroutine MissileLock. Is that a good idea?
    • what happens if a target becomes invisible in the second during which you wait in the coroutine?
    • When you say 'it doesn't work' - can you describe the setup, and how you know that 'it' doesn't work (i.e. what did you expect to see, what are you seeing instead and why do you think that is wrong)?
     
  5. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    @PixelCloudInc Don't get me wrong, but you just asked for debuging random piece of code with no context - this is not how it should be done. If you want to get precise or at least helpful answer, you need to ask precise questions.
    First of all no one here have idea how exactly your code is made, what and where calls this method and how many times, so you can't ask questions that only you know the answer, unless you provided good context / example.
    Second, you never explained where this script is attached and how many times and this makes big difference.
    Also you didn't provide information why these variables are there and what they do or how they are set.
    Third, as csofranz said - do not randomly assume what part of code is broken. Obviously you can suspect it, but there might be hundred of reasons for this to happen.

    You should make it this way:
    • describe what you want to do and provide context how it's made (objects on scene, scripts, anything essential)
    • describe what should happen and what happened
    • add some of your observations that might help us to find the answer
    • attach possibly broken code and explain why you think it's broken
    • and the most important: describe important parts of your code and what they are supposed to do - at this point you even might find the answer yourself and if not, other people can easy find errors in your logic.
     
  6. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    OnbecameVisible/OnbecameInvisible are MonoBehaviour functions called when the renderer became visible by any camera. He don't call it himself.
     
  7. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    Oh this is right, but then it would mean he enables missle lock when camera need to render it? This sounds a bit broken to me unless you want missles to follow you when you watch them (assuming camera is attached to player).
    In this case every time you can't see one of these objects missile stops working.
     
  8. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    Right I understand, it's just my grandmother passed away and i haven't time to really do anything else for the past 24 hours so this post was kinda rushed...
    -I have a missile which I want to rotate towards my target.
    -However I want this target to change dynamically at runtime.
    -This script which i posted is the code I have for the locking part.
    -It goes on any object that i want to allow to be targeted.
    -it basically uses OnBecameVisible to see if the object is in the player's view, then does a distance check to see if the player is in range, then sets a timer after which i simply set the object to be the missiles target.
    -The script works fine only when there is one object in the scene which the script is sitting on, However if I add any more than that this script stops working.
    Hope this is enough.
    I also cannot bring out any particular lines of code that I don't think function properly because it works fine with a single gameobject.
     
  9. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    T
    Yes that is intended.
     
  10. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    Yes i used it because i didn't want to use Raycast Checks
     
  11. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Please describe a Little bit closer what you mean by "however, if I add any more than this script stops working.

    If I understood you correctly, you'll now have multiple instances of that script in your scene, once for each enemy, attached to each enemy. How do you know they aren't working? Taken two enemies, what do you expect to happen
    - when none is visible, in range
    - when one is visible, but not the other, all in range
    - when one is invisible, but the other is visible, all in range
    - when both are visible, all in range
    - when both are visible, all out of range
    - when both are visible, first in range, the second not in range
    - when both are visible, the first not in range, second in range

    And what do you get instead?
     
  12. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    Assuming there is only one GuidedMissileController, then if any of targets is not visible you can end up with LockCheck = false; and this resets your target to null. Did you try to debug what actually happens? For example put Debug.Log("hello") in line 30 to see if it's reset all the time or line 43.
    I never used these callbacks, but maybe editor camera affects what is rendered too and call these callbacks?
     
  13. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    When one is visible, that respective target is in range if it is less than 1500m from the player.
    But with more than one instance none of this happens
     
  14. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    When i get home ill try debig.log to see what happens
     
  15. Divis10nStudios

    Divis10nStudios

    Joined:
    Dec 26, 2017
    Posts:
    197
    Nvm fixed it... used Raycasts to check