How To create 2D collision detection on object which changes dimensions
Hi, Im new to Unity Development and wanted to know what the best method is to solve the problem below.
Problem: I am working in 2D and my player can fire a spear from a spear gun with a chain connected (preview included from java version below). This chain is collidable and increasing in height constantly (until it collides with something or the edge of the screen). My issue is the standard collider doesnt support directly changing its dimensions/shape once its already set and the solutions i do know of (and have implemented in java etc before) feel like hacks and usually involve messy direct handling of the box2D collider.
Solutions i am considering/aware of: I solved this issue in java on an older version of the game about a year ago (but never finished the game and felt unity would be better now i have the time to do it again) so i have a few solutions i can think of. some i know work in java at least (where i have direct access to the collision engine) others are less ideal backup ideas.
Solution 1: each frame in the backend code where the spear is updated, destroy its collider and create a new one of the correct size *last time i did this in box2D java it required a workaround to get it to work correctly as box2D does not detect collisions on the first update after the collider is added and as the collider will always be renewed each update it never collides, im not sure this workaround is possible/needed in unity
Solution 2: implement my own physics throughout the game. This is allot of hassle to get looking good and feels wasteful when one already exists that i should be able to work with
Solution 3: somehow create two physics systems one hand made for the spear and use unity for the rest and somehow hook into the unity collision system to integrate the two. I think this is possible from what ive seen online but ive no idea where to start with it.
Solution 4: instead of working with a single collider and changing its shape, create a chain of multiple smaller colliders and spawn them as needed. the issues with this are balancing performance and accuracy, the smaller they are the more accurate collisions will be but the simulation will then need to handle far more colliders and when the object is destroyed suddenly clear out many, many colliders all at once which im uncertain is a good idea or the colliders are larger but this creates a greater error margin during gameplay. i should note there may be more than one spear on the screen at once, eventually the game should at least be capable of many many spears being shown with little to no performance impact.
So how in unity do you solve the problem of a collider changing dimensions each frame or update? Do i use one of the above solutions? if so advice might be needed on how to even do it (i know how to do allot of these solutions when im working low level with direct access to the underlying physics system but in unity im uncertain). if not does anyone have any other suggestions/advice on how to handle it?
Thanks in advance everyone
Answer by roblind3 · Dec 14, 2016 at 01:15 AM
So not exactly sure what you're doing to get the chain to increase length indefinitely...but I do know that you can adjust the size of the Collider with code like this: GetComponent<BoxCollider2D>().size = new Vector2(sizeX, sizeY);
That's assuming you're using a 2D Box Collider...but you can replace that any type of Collider. In another part of your code, you can set the "sizeX" and "sizeY" floats to match how long your chain has gotten. But since I don't know how you're causing the chain to extend, I'm not sure how to find your length of chain. Let me know if that helps!
Thanks for that, i swear i tried this a couple years ago when i started this game and it didn't work, hadn't thought to recheck it worked.
Your answer
Follow this Question
Related Questions
How to make object with velocity bounce off the obstacles? 0 Answers
Physics 2D with tile collider corner problem 2 Answers
Ignore collision based on position 1 Answer
GameObject with multiple collider2D, 1 Answer
Collision check 0 Answers