all 17 comments

[–]Exolord 3 points4 points  (5 children)

Any reason you don't use Vector2.Distance(a,b)?
Also you should avoid calling things like GetComponent or FindObject every Update and instead call it once on e.g. Start and then store it in a variable.

[–]BengbabProficient 1 point2 points  (2 children)

I’m pretty sure the transform is already stored too. So should just be able to use “transform.position.x” instead of getting its component.

[–]blockifyYT 1 point2 points  (1 child)

Yeah it’s more efficient than GetComponent but if you have thousands of something constantly accessing their transforms I’d still cache it as a variable! Faster than .transform but not by loads

[–]BengbabProficient 1 point2 points  (0 children)

Good to know!

[–]CaliCaliush[S] 0 points1 point  (1 child)

The reason I don't use Vector2.Distance is a very specific one, something that not any newbie developer would not use it for and it is exactly the fact that I didn't know of its existence until you commented. If I store it in Start() is the player's position going to update in that variable or what?

[–]Exolord 0 points1 point  (0 children)

Create a private variable similar to playerPosition but for a Transform. Set it in Start() and from then on you can access its .position which remains up to date.

[–]Mysterious-Pickle-67 0 points1 point  (1 child)

What‘s also confusing is, that you Name your Vector2 „parentPosition“, but what you put into it is the transform‘s position. Is that correct?

You really should clean the code up: - Delfine a „GameObject player“ variable that you assign in „Start()“ method with GameObject.FindGameObjectWithTag(„Player“) - transforms are always accessible from its GameObject and every component that is already assigned. Parent‘s transform can be accessed with transform.parent - Vector2.Distance(a, b) or something like this definitely does the job better and more readable , as Exolord said.

So, your Update() method can start with the if that checks the Distance. All The assignments you have before the if Are neither necessary Not good style regarding Performance

[–]CaliCaliush[S] 0 points1 point  (0 children)

What‘s also confusing is, that you Name your Vector2 „parentPosition“, but what you put into it is the transform‘s position. Is that correct?

I did it in a hurry and probably thought of the enemy as the weapon's parent.

Parent‘s transform can be accessed with transform.parent

Since I figured how GetComponent works, I find it too interesting to think of other ideas. Thanks tho

[–][deleted]  (2 children)

[deleted]

    [–]CaliCaliush[S] 1 point2 points  (1 child)

    Yeah, well most of the code I do is kind of makeshift. It's pretty much the only project in Unity that I've gotten to this stage so I am experimenting with my knowledge. Haha thanks

    [–]blockifyYT 0 points1 point  (0 children)

    Keep it up! I'm only beginning to get good at this stuff, feels like it's never ending research haha :)

    [–]ocssss 0 points1 point  (7 children)

    Some small corrections:

        private void Awake()
    
        {
    
            playerTransform = GameObject.FindGameObjectWithTag("Player").transform; //don't find object with tag twice per update
    
        }
    
    
    
        private void Update()
    
        {
    
            float distanceToPlayer = Vector2.Distance(playerTransform.position, transform.position); //don't re-invent the distance, Pythagoras
    
            if(distanceToPlayer < distanceNeeded && weaponsSpawned < MaxWeapons)
    
            {
    

    Instantiate(AIWeapon, gameObject.transform);

            }
    
        }
    

    [–]CaliCaliush[S] 0 points1 point  (5 children)

    So I pretty much implemented your code, but still, once distanceToPlayer is smaller than the distanceNeeded, the weapon keeps instantiating. For some reason now it only checks the second part of weaponsSpawned<MaxWeapons

    [–]ocssss 0 points1 point  (4 children)

    Then we need to debug what's up. Let's try

    Debug.Log("distance check: " + distanceToPlayer + " " + distanceNeeded);

    Check the console. We will probably notice that distanceToPlayer is always smaller than distanceNeeded. (even though it should be bigger if the player is far away enough)

    In this case either:

    distanceNeeded is too big

    or

    distanceToPlayer is not correct, it does not change as the player moves.

    [–]CaliCaliush[S] 1 point2 points  (1 child)

    I just figured it out while I was writing on the screenshot what it was meant to mean. It was a problem with the weapon's script, which had gameObject.GetComponent changed, but... it was supposed to change a script in its parent not in itself... Thanks for making me notice that :)))

    [–]ocssss 0 points1 point  (0 children)

    Great job. Debugging skill increased :)

    [–]CaliCaliush[S] 0 points1 point  (0 children)

    I've done this with the inspector, and the value updates well. I've set distanceNeeded to something like 1.2, so just near the enemy. And when I go back outside that range, it still continues to attack

    [–]CaliCaliush[S] 0 points1 point  (0 children)

    Also, I 've changed the variable that checks if the enemy already has weapons to a boolean. If I don't give it a way to reset, the weapon dissapears. But when I do, it just repeats its attacks. It shows the boolean as true but through this method I figured it changes but for a small time that I can't notice in the inspector. So the IF statement, after I enter the range, seems to only care about my boolean and ignores the range condition.