This week, we worked to make the “Gem Found” prefab that we created last week actually appear when we found a gem, and disappear after it’s played it’s animation.
A Script for Our ‘Gem Found’ Prefab
We want a simple script, called GemFoundMessage.cs, that will set the message on the prefab’s canvas to match the gem we just found:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class GemFoundMessage : MonoBehaviour
{
public TMP_Text Text;
public Transform GemHolder;
public void SetGemDefinition(GemDefinition gd)
{
string message = string.Format("You found a {0} spirit gem!",
gd.name);
Text.text = message;
}
}
We attach this to the root of of our prefab. This script contains a single function we can call to set the text based on the gem definition and a couple of properties. One property is the text we need to update, the other is the location where we’re going to spawn a gem model to have it animate.
Making the GemFound Prefab Clean Itself Up
We already have a SelfDestruct script. We add it to our GemFound Prefab and give it a time of 4 seconds; this is enough time for our animation to finish.
Updating PlayerDig
We now need to update PlayerDig.cs. There are a few things to do:
- After spawning our digging animation, we want to wait three seconds and then check to see if we’ve found a gem.
- We look to see if the nearest gem is within a certain distance (the larger we make this the less precise the player will need to be)
- Assuming there is a gem there, find out what type of a gem it is
- Add it to our inventory [We’ll need to wait to do this, as we don’t have our inventory written yet]
- Create a GemFound prefab
- Inform it what gem type we have
- Create a gem as part of the GemFound
First we add the line Invoke(“DigResult”, 3); this to the OnDig() function as shown:
:
if (Physics.Raycast(rayPos, Vector3.down, out hitInfo, 10.0f, lm))
{
Instantiate(DiggingPrefab, hitInfo.point,
DiggingPrefab.transform.rotation);
Invoke("DigResult", 3);
}
:
We then add the DigResult() function:
private void DigResult()
{
float distToNearest = (ItemScatterer.nearestItem.transform.position -
transform.position).magnitude;
if (distToNearest > MaxDistToItem)
return;
// Capture the gem definition
RandomGem rg = ItemScatterer.nearestItem.GetComponent<RandomGem>();
GemDefinition gd = rg.GemDefinition;
// Remove the item from ItemScatterer
Destroy(ItemScatterer.nearestItem);
ItemScatterer.nearestItem = null;
// TODO: Add to our inventory
// Spawn the Gem Found Prefab to let the player know what they found
GameObject gfb = Instantiate(GemFoundPrefab, GemFoundLocation);
GemFoundMessage gfm = gfb.GetComponent<GemFoundMessage>();
GameObject gem = Instantiate(gd.Prefab, gfm.GemHolder);
gfm.SetGemDefinition(gd);
}
This does everything we talked about above.
Basic UI
We took a short look at simple UI layout options as preparation for building the UI next session.
Code Download
The code for this week’s project is on our GitHub, as always.