Skip to content

Purchase Vehicle NFTs from an in-game marketplace and start earning ERC20 tokens used to buy upgrades!

Notifications You must be signed in to change notification settings

kryan19k/unity-karting-game

 
 

Repository files navigation

Karting Game - Unity

Build a karting game in Unity using GamingKit featuring:

Preview: Play the game! Karting Game Demo

How it works

Below, we'll outline the web3 features of the game enabled by GamingKit.

You can learn the features of the karting game itself from Unity Learn: Karting Template.

Connect Wallet

New users first connect their wallet to the game:

Demo Preview 0

This is achieved within the Web3.cs script:

using Thirdweb;
using System.Threading.Tasks;

public class DemoClass : MonoBehaviour
{
    // Store the sdk so we can use it in any method of this classjb
    private ThirdwebSDK sdk;

    void OnEnable()
    {
        // When this becomes active, instantiate the SDK
        sdk = new ThirdwebSDK("optimism-goerli");
    }

    // When the user clicks "Connect Wallet":
    // - Ask them to connect their wallet
    // - Ask them to switch to Optimism Goerli network
    public async Task<string> EnsureWalletState()
    {
        string address = await sdk.wallet.Connect();
        await sdk.wallet.SwitchNetwork(420);
        return address;
    }
}

Buying Vehicle NFTs

On the marketplace there is a listing for the two vehicles:

Listings

On the screen after the connect wallet button, the user can buy these vehicles using their $PIN token:

Demo Preview 1

The first kart is free (0 $PIN), and the second kart costs 25 $PIN.

To start the game the player purchases the blue kart with the below method:

private Marketplace GetMarketplace()
{
    return sdk
        .GetContract("0x9b5283690D3161e61557b929C5846b1259c50693")
        .marketplace;
}

public async Task<TransactionResult> BuyItem(string listingId)
{
    var result = await GetMarketplace().BuyListing(listingId, 1);
}

When the transaction goes through, the button changes to display the Drive Vehicle text, and the player can begin to play the game with the purchased vehicle NFT.

Core Gameplay

The core gameplay and assets are available as part of the Unity Learn tutorials:

Earnable $PIN Tokens

When the player wins, they are taken to the WinScreen, where they can claim 25 $PIN tokens.

This is achieved using the Token Drop Contract which is a way of releasing your ERC20 tokens.

Note: Users can claim from the contract directly in this example. In a real game, you'd need to check the user has won the game before allowing them to receive tokens.

The logic for this can be seen in the TokenClaimer.cs script:

private Contract getTokenDrop()
{
    return sdk.GetContract("0x4a9659d5E0d416Ce8B9a4336132012Af8db4c5AB");
}

public async void Claim()
{
    await getTokenDrop().ERC20.Claim("25");
}

At this point, the user can return to the marketplace and purchase the second vehicle for 25 $PIN. They can then drive the second vehicle and repeat the process.

Got Questions?

Jump into our Discord to speak with our team directly!

About

Purchase Vehicle NFTs from an in-game marketplace and start earning ERC20 tokens used to buy upgrades!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 60.5%
  • ShaderLab 16.4%
  • Mathematica 15.8%
  • HLSL 2.8%
  • HTML 2.6%
  • CSS 1.0%
  • JavaScript 0.9%