[HOWTO] How To: Extend Surface Data

Announcements, support questions, and discussion forum for Grid Controller.
Post Reply
User avatar
Tony Li
Posts: 22083
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Extend Surface Data

Post by Tony Li »

Grid Controller detects Surfaces on floors, walls, and ceilings.

Version 1.0.10 adds a GridController.CurrentFloorSurface property that returns the Surface under the player's feet.

In version 1.0.9 and earlier, you can make a subclass of GridController and override GetSurfaceUnderPosition to get the Surface, something like:

Code: Select all

public class MyGridController : GridController
{
    public Surface CurrentFloorSurface {get; set;}
    
    protected override Surface GetSurfaceUnderPosition(Vector3 position, out Vector3 hitPoint)
    {
        CurrentFloorSurface = base.GetSurfaceUnderPosition(position, out hitPoint);
        return CurrentFloorSurface;
    }
}
(I just typed that in here; there could be typos, etc.)

Version 1.0.10 is coming out on Monday (as of this post), so it might be simpler to just wait for it.

Whether you use 1.0.9 or 1.0.10, you can make a subclass of Surface, or a subclass of the SurfaceType ScriptableObject asset class, to add your own data. For example, if you want to damage the player when they step on certain squares, you could make a subclass of SurfaceType:

Code: Select all

[CreateAssetMenu]
public class DamagingSurfaceType : AudioSurfaceType
{
    public int damage = 0;
}
Create ScriptableObject assets of this type and assign them to Surfaces. Then you can hook into GridController's EnteredPosition event:

Code: Select all

GridController.Instance..EnteredPosition += CheckSurfaceOnEnteredPosition;
...
void CheckSurfaceOnEnteredPosition(Vector3 position)
{
    if (GridController.Instance.CurrentFloorSurface.SurfaceType is DamagingSurfaceType damagingSurfaceType)
    {
        TakeDamage(damagingSurfaceType.damage);
    }
}
Post Reply