Unity Cookbook
  • 2D
    • New Input System - 2D Move
    • Pixel Sprite Setup
    • Animations
    • Animator in Script
    • Tilemap
    • 2D Camera Follow Player
  • UI OnlyFans
    • 9-Splice Sprite
    • Interactible Game Objects
      • NPCs
      • Items
      • Interactible Marker
    • Dialogue System
      • Conversa Plugin
      • DiaManager.cs
      • DiaTrigger.cs
      • DiaManager.cs Alternate
      • Choices in Dialogue
    • New Input System - Interact
    • Designing the General Menu
    • New Input System - Open Menu
      • UI Buttons
      • GeneralMenu.cs
    • Controls Explanation UI
    • Items Menu
Powered by GitBook
On this page
  • Setup
  • Coding the Script
  • Animation
  • Make a Prefab
  1. UI OnlyFans
  2. Interactible Game Objects

Interactible Marker

PreviousItemsNextDialogue System

Last updated 1 year ago

Working File:

Setup

Change the color of the static sprite to have 0 Alpha. Remove the Rigidbody2D and click on "Is Trigger." Edit the collider so that it accurately tells the range.

Now, just drag that UI sprite for the marker to the location you want it in the scene.

Then, make that marker sprite a child of the Interactible object.

Coding the Script

This is going to be very simple. We will simply enable the marker when the player enters the trigger zone. You want to attach this below script to the Interactible gameObject in the scene.

using UnityEngine;

public class InteractibleMarker : MonoBehaviour
{

    [SerializeField] private GameObject marker;

    private void Start()
    {
        marker.SetActive(false);
    }

    //Upon collision with another GameObject
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            marker.SetActive(true);
        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            marker.SetActive(false);
        }
    }

}

Then, from the inspector, pull the marker sprite into the script component.

Animation

Depending on the style, you may want the marker to move. With the marker sprite selected in the scene hierarchy, create a new animation. Go to the time you want the thing to change, and add transform property. Create a new keyframe with the changed values. If needed, use the dopesheet to make sure it is undulating correctly.

Make a Prefab

Now, we can simply drag this as a child of the gameObjects and have it do all the same stuff all over again.

To make a prefab, pull that newly created gameObject in the scene view and pull it into the projects window of the folder of your choosing. I created a "Prefabs" folder.