How to access PolygonCollider2D.points

Hi!

I have another problem, for some reason, I can’t access PolygonCollider2D.points[whatever] variable. Don’t you know how to use those variables properly? Do I need to predefine them?
PollygonCollider2D.GetTotalPointCount() does not work for me too.

Thanks for any tips.
Have a nice day!

My specific problem is, I can’t set new position of collider’s point. I tried this line:

_polygonCollider.points[0].Set(1.0f,1.0f);

or this one:

_polygonCollider.points[0].x=0.1f;

None of those works. Have you got any idea why?
Thanks for any reply…

You should get the points array, change points, then assign back. Or just create the array from scratch and assign it.

–Eric

Well, first, I defined this array:

Vector2[] _points;

Then I try this at Start:

void Start () {
		_polygonCollider = gameObject.GetComponent<PolygonCollider2D>();
		numberOfPoints = _polygonCollider.GetTotalPointCount();
		Vector2[] _points = new Vector2[numberOfPoints];

		for(int i=0;i<numberOfPoints;i++){
			_points[i] = _polygonCollider.points[i];
		}
	}

And this is in Update, just testing changing position:

for(int i=0;i<numberOfPoints;i++){
			_points[i].x += 2;
			_polygonCollider.points[i]=_points[i];
		}

I probably do not understand arrays properly. Do you think you could help me with this a litle bit? Thank you very much…

var myPoints = polygonCollider.points;
// do stuff with myPoints array
polygonCollider.points = myPoints;

–Eric

2 Likes

Solved. Thank you so much for help!

Is there a finite number of times a PolgonCollider2D can be added and destroyed? For some reason our sequence works fine for a limited period, then freezes. Is the destruction of those subject to a race condition that we can flush or wait for?

Eventually you would run out of memory, unless you clear unused assets with Resources.UnloadUnusedAssets. It would probably be better to not create/destroy polygon colliders, but just change the points instead.

–Eric

Is there a way to change collider points without adding a new PolgonCollider2D? I gather setPath would do that, but the new points around the sprite are provided by the add PolgonCollider2D. Is there another way to extract those points from the sprite?

You can make new a new points array, along with using pathCount and SetPath. You can use GetPath to see the existing points.

–Eric

Can PolygonCollider2D.GetPath be called with a new sprite, and the original collider?

I don’t quite know what you mean. GetPath gets the points in a path for a polygon collider; what you do with that array is up to you.

–Eric

The collider also serves to extract the boundary of the sprite. Is there an explicit, transient function to do that so the same collider can keep getting updated points?

Do you mean you’re constantly destroying the collider so you can build a new one with the new auto-generated shape? That’s definitely not efficient; even if there was an AutoComputeColliderFromSprite function (which sadly there isn’t, or at least it’s not public), it would be faster to keep the auto-generated colliders in arrays, and apply them as needed. Although that will only work if you have a set number of pre-defined shapes (like a series of animated frames).

–Eric

It is a continuous number of organically generated shapes. We can create our own evaluator if necessary, but it would be more efficient to access the private method already used by the collider if it can be exposed. I assume we can then keep resetting the path in a single collider.

Is there a way to access the internal, (AutoComputeColliderFromSprite function), sprite evaluator? Does anyone know the actual name for the private function in PolygonCollider2D that performs that function?