Skip to content

SnapPointMaker

AzumattDev edited this page Jul 26, 2023 · 1 revision

SnapPointMaker

Overview

SnapPointMaker is a static class used to create snap points on GameObjects in Unity. A snap point is a specific point on a GameObject where other objects can "snap" or attach to, based on a game's mechanics.

Supported collider types are BoxCollider and SphereCollider. You can specify the types of snap points you want to create using the SnapPointType enumeration.

Usage

Add Object for Snap Points

To register a GameObject for snap points creation, use the AddObjectForSnapPoints method:

SnapPointMaker.AddObjectForSnapPoints(myGameObject);

Apply Snap Points

To apply snap points to all registered GameObjects, use the ApplySnapPoints method:

SnapPointMaker.ApplySnapPoints(SnapPointType.Vertices | SnapPointType.Center);

This example will apply snap points at the vertices and the center of the registered game objects, given they have either a BoxCollider or a SphereCollider component.

SnapPointType Enumeration

SnapPointType is an enumeration used to specify the types of snap points to create:

  • Vertices: Snap points will be created at the vertices of a BoxCollider.
  • Center: A snap point will be created at the center of the collider.
  • Poles: Snap points will be created at the top and bottom of a SphereCollider.
  • Equator: Snap points will be created at the cardinal directions on the equator of a SphereCollider.

These enumeration values can be combined using the bitwise OR operator (|). For example, SnapPointType.Vertices | SnapPointType.Center will create snap points at both the vertices and the center.

Note

The SnapPointMaker currently supports BoxCollider and SphereCollider types. If your GameObject has a different type of collider, you will need to provide additional logic to determine the snap points. The Unsupported collider type warning will be displayed in the console for unsupported collider types.

Improvements

The snap points created by the SnapPointMaker are static and do not adjust dynamically if the GameObject changes shape or size. If you require dynamic snap points, you'd have to implement a system that updates snap points in response to changes to the GameObject.

Clone this wiki locally