#Help with finding enemy colisions

1 messages · Page 1 of 1 (latest)

toxic bronze
#

.

#

you should be passing a specific position to check for an overlap at, such as..

Physics2D.OvelapCircleAll(Vector2.one, 1f);
Physics2D.OvelapCircleAll(new Vector2(-1, 1), 1f);```
these are all valid
brazen relic
#

Before anyone gives more hints. Look at the code Null gave u long ago. Try to use it and then show us the errors

#

Stop guessing what to write

rotund vale
#

i would rather just work through it directly.

toxic bronze
#

wait it's physics 2d not physics

#

bruh

rotund vale
#

oops

#

but yes, it's Physics2D.OverlapCircleAll

peak wave
#

I even changed my example to 2D

rotund vale
#

they will be very similar

#

Physics.OverlapSphere and Physics2D.OverlapCircle behave almost identically

rotund vale
toxic bronze
#
        foreach (Collider obj in objs)
        {
            if(obj.TryGetComponent(out Enemy enemy))
            {
                if(!enemiesList.Contains(enemy))
                enemiesList.Add(enemy);
            }
        }``` got the fcode
rotund vale
#

your brain can be surprisingly good at smoothing over typos

#

anyway, when you wrote Physics2D.OverlapCircleAll(Vector2, 3.5f), you were not passing a value

#

now you are, indeed, passing a value to the method

peak wave
rotund vale
#

transform.position, specifically, which is a Vector3

#

this can be implicitly converted to a Vector2 by dropping the Z-coordinate

#

therefore, this is a valid use of the method

peak wave
#

foreach (Collider obj in objs)
this needs to be
foreach (Collider2D obj in objs)

rotund vale
#

indeed: you should be getting a type error there

#

objs has no way to produce a enumerator of Collider. it only knows how to make an enumerator of Collider2D.

brazen relic
#

And if your enemy script is not specifically named Enemy, it wont work. You need some distinction between a collider of an enemy and the collider of a wall or box.

toxic bronze
brazen relic
#

So that part must either change to a different component name or tags

toxic bronze
#

oh

peak wave
brazen relic
#

Its targeting a component, and that component would be c# code yes

toxic bronze
#

The only thing it asks now is to make list, I use "object" for that right?

peak wave
brazen relic
#

Wait why are u making a list in there now

peak wave
peak wave
brazen relic
#

The initial problem was different, he wanted something to bounce between each collider. So now u must just bounce the bullet on the direction of each object

toxic bronze
toxic bronze
timid viper
#

You can break out of the loop because you want to ricochet to one enemy at a time

peak wave
#

Ah ok

brazen relic
timid viper
#

And the next ricochet your bullet knows what enemies it already has hit, so it wont go back to those again

brazen relic
#

U want on the first collision of the bullet to get a list of nearby enemies
Then it will bounce to ever enemies. U do not want it to happen on every single collision

toxic bronze
brazen relic
#

Before u implement a limit of 3 bounces, try to get it to bounce at least once

#

Or to everything nearby

toxic bronze
#

I'm just re-reading everything to make sense of it

#

I got a list of things I need to learn about, when the jam ends I'll search up all those topics

#

The list is big so it's going to take me some time

#

How do I debug an array like this to see what's not working?

rotund vale
#

in what way is it not working?

#

are you getting no hits?

#

it would be reasonable to put a log statement in the loop

#

to see what (if anything) is showing up

toxic bronze
rotund vale
#

hm, I'm actually not sure.

#

the 3D overlap methods have a QueryTriggerInteraction parameter

#

which lets you decide if trigger colliders should be included or not

#

but OverlapCircleAll does not have such a thing

toxic bronze
rotund vale
#

if you want to detect when you've overlapped a trigger collider, I'd just use the OnTriggerEnter2D message.

#

consult this

brazen relic
#

Your enemies can have triggers and colliders on them, although im not sure why you would have a trigger as triggers do not stop on collision

#

This means you would walk through them, also if they have gravity I'm surprised they arent falling through the floor

timid viper
#

Physics.queriesHitTriggers exists too btw, you can set it to true temporarily before the overlap and false afterwards

#

But QueryTriggerInteraction can do that too if im not mistaken

toxic bronze
brazen relic
#

You may want to consider if u do want a collider or trigger still

#

But as another said u can still have it work with triggers. I just feel it's a hacky solution when it shouldnt be a trigger

toxic bronze
#

I would love to only work with triggers, but it appears there's some things that are only possible with colliders

timid viper
#

You can use either here

#

All of the physics queries (raycast, overlaps etc) work with triggers if you tell them to

toxic bronze
#

I started not that long ago

toxic bronze
toxic bronze
#

I'm back

#

Tried to apply the querry thing and I had no compiling errors, but it's not lerping or changing direction to the position so I still gotta think about that

brazen relic
#

U havent given it a direction after finding an enemy

toxic bronze
brazen relic
#

Plus im not really sure what the colliders list is for. Not sure if someone else recommended it but u really dont need it since the overlap circle thing gives u unique colliders

#

Unless u are trying to make it so it doesnt hit every body part before moving on

toxic bronze
#

Does that mean that's getting the references?

#

The function is being called

brazen relic
#

Show the line for that error

#

Line 40

toxic bronze
#

Add(enemy)

brazen relic
#

That error means that an enemy it hit wasnt initialized, not entirely sure why that's happened because I never used trygetcomponenet. Give me a sec to get on my pc if no one answers before me

toxic bronze
brazen relic
#

I believe u need to specify a component

toxic bronze
#

What do you mean by that?

brazen relic
#

not sure what its doing when u dont specify one, but it looks like its grabbing any component it hits

#

look at the example in the doc i sent u

#

and compare back to what u have for TryGetComponent

toxic bronze
#

wait

#

no did not work, same error but with line 39 because I deleted a comment.

#

Maybe it's waiting for an instanciated version of the same object?

brazen relic
#

i guess that part didnt matter, not too sure personally since i dont use TryGetComponent. Try debugging what enemy actually is. Because the issue rn is that its just null

#

oh wait maybe its an issue that the list wasnt initialized yet

toxic bronze
#

I hope not

brazen relic
#

it is

toxic bronze
#

it's not

#

i hop

#

e

brazen relic
#

do u know how to initialize a list

toxic bronze
#

Like this?

#

wait...

#

hmm

brazen relic
#

almost, u dont need to redeclare what the type is

#

also i think c# just lets u do
enemylist = new();
now

toxic bronze
#

it doesn't let me do this, the other way around works though

#

Same problem at the same line

#

I don't know why thi !enemies list doesn't make too much sense in my head

#

like, what's actually doing?

brazen relic
#

u can think of ! as a negative. "If not enemies list contains enemy..." means "if the enemy isnt in the list"

toxic bronze
#

yeah, ! is negative

#

But what if we already got a enemy? then it never adds a new enemy?

brazen relic
#

no its just checking if the new enemy its comparing to is not already in the list

toxic bronze
#

hm then why there's a instance error everytime on this specific line saying that a object reference is not set as a instance?

brazen relic
toxic bronze
#

fuck

brazen relic
#

above the !enemiesList if statement, can u debug what enemy.transform.name is? thatll give u an indicator as to what its checking as an enemy

toxic bronze
#

apparently I'm using C#8.0

brazen relic
#

it shouldnt matter

#

writing new() instead of new List<...>(); is the same but its just condensed. Doesnt change anything

rotund vale
#

yeah, target-typed new used to not work in Unity, back before it got to a new enough version of C#

toxic bronze
#

i think?

brazen relic
#

what else are u printing, because either something printed 9312 times or something is very broken

#

also im guessing u have 57 enemies named Enemy(1)?

toxic bronze
brazen relic
#

the game shouldve completely stopped on the first error

#

im confused how u have 16 errors

rotund vale
#

it can only pause at the end of the frame

#

Debug.Break() doesn't stop instantly, for example

toxic bronze
#

realtime run

rotund vale
#

also, error pause is off, so I wouldn't expect it to stop :p

brazen relic
#

ah

#

yes turn on error pause next to collapse

toxic bronze
#

done

#

.... what?

#

no error?????

#

I don't get it

#

I added more enemies, the code seems to work (not ricochet) but doesn't give errors

brazen relic
#

you mightve just not saved previously then

#

because i see no errors in the code

toxic bronze
#

Prefabs

brazen relic
#

you seem to not have a problem right now, it looks like its doing whats intended

toxic bronze
#

I feel accomplished but empty at the same time

#

I'm coding for 24 hour straight

#

Any tips on how to make the bullet move to their positions? should I just use lerp?

brazen relic
#

thats up to u, how are u moving bullets in the first place

toxic bronze
brazen relic
#

then use addForce to continue richocheting

#

just in the direction of the next target

toxic bronze
#

so rb.AddForce(enemy.Position * bulletForce, ForceMode2D.Impulse);?