#Help with finding enemy colisions
1 messages · Page 1 of 1 (latest)
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
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
i would rather just work through it directly.
i just noticed a typo in there
oops
but yes, it's Physics2D.OverlapCircleAll
I even changed my example to 2D
they will be very similar
Physics.OverlapSphere and Physics2D.OverlapCircle behave almost identically
the common thread here is that we are passing specific values to the method
I'll register myself as legaly blind after this one
foreach (Collider obj in objs)
{
if(obj.TryGetComponent(out Enemy enemy))
{
if(!enemiesList.Contains(enemy))
enemiesList.Add(enemy);
}
}``` got the fcode
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
I changed it again to use OverlapCircleAll
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
foreach (Collider obj in objs)
this needs to be
foreach (Collider2D obj in objs)
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.
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.
yeah realised this one too
It's targeting the script?
So that part must either change to a different component name or tags
oh
Enemy script must be on the collider or it wont detect Enemy
Its targeting a component, and that component would be c# code yes
The only thing it asks now is to make list, I use "object" for that right?
private List<CubesBehavior> enemiesList
Wait why are u making a list in there now
I did say don't copy exact 🙂
idk I just assumed in case they wanna do something with all the detected enemies at later point
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
well I did not know what changes to make to make it work
If I code the bullet to go very fast it's fine no?
You can break out of the loop because you want to ricochet to one enemy at a time
Ah ok
gotcha
Work out mentally what the logic would be. If u dont understand what u are trying to do mentally, u wont understand what u want to do in code.
And the next ricochet your bullet knows what enemies it already has hit, so it wont go back to those again
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
It'll destroy the bullet after 3 bounces
Before u implement a limit of 3 bounces, try to get it to bounce at least once
Or to everything nearby
Yes that's the plan I'll try to execute the code now
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?
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
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
Would this code work with triggers or only with physic objects?
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
After some troubleshoothing, I realized it does not work with triggers, I'll have to rework my code so enemies also take damage by colisions
if you want to detect when you've overlapped a trigger collider, I'd just use the OnTriggerEnter2D message.
consult this
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
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
The concept of the game is that you're stopped in place and build up stronger as time goes unlocking weapons and passive upgrades to increase the fun
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
I would love to only work with triggers, but it appears there's some things that are only possible with colliders
You can use either here
All of the physics queries (raycast, overlaps etc) work with triggers if you tell them to
I have only a few hours till I upload the game, to be honest I'm proud of myself by making almost all of it with virtually no help (for exception of the last 7 hours trying to figure how to bounce stuff)
I started not that long ago
hmmmm I'll eat first to see if I can understand how to apply this
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
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
U havent given it a direction after finding an enemy
Yeah I'm thinking on how to do that now
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
I think is to get a reference easier?
Does that mean that's getting the references?
The function is being called
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
Add(enemy)
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
So funny like such a little mechanic can give so much trouble sometimes
What do you mean by that?
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
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?
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
...................................................................
I hope not
it is
do u know how to initialize a list
almost, u dont need to redeclare what the type is
also i think c# just lets u do
enemylist = new();
now
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?
u can think of ! as a negative. "If not enemies list contains enemy..." means "if the enemy isnt in the list"
yeah, ! is negative
But what if we already got a enemy? then it never adds a new enemy?
no its just checking if the new enemy its comparing to is not already in the list
hm then why there's a instance error everytime on this specific line saying that a object reference is not set as a instance?
if it works with new List<...>(); then use that but i find that odd because mine works. Maybe its a newer verison of c#
I have no errors for that but im not 100% paying attention if u have the exact same syntax too
fuck
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
apparently I'm using C#8.0
it shouldnt matter
writing new() instead of new List<...>(); is the same but its just condensed. Doesnt change anything
yeah, target-typed new used to not work in Unity, back before it got to a new enough version of C#
it did identify the enemies
i think?
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)?
the object lol
I think it's uploading each frame
the game shouldve completely stopped on the first error
im confused how u have 16 errors
it can only pause at the end of the frame
Debug.Break() doesn't stop instantly, for example
also, error pause is off, so I wouldn't expect it to stop :p
done
uhh
.... what?
no error?????
I don't get it
I added more enemies, the code seems to work (not ricochet) but doesn't give errors
I think I know the root of the problem
Prefabs
you seem to not have a problem right now, it looks like its doing whats intended
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?
thats up to u, how are u moving bullets in the first place
very simple code
then use addForce to continue richocheting
just in the direction of the next target
so rb.AddForce(enemy.Position * bulletForce, ForceMode2D.Impulse);?