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
  • Fixing Stupidity
  • Testing
  • Look Direction
  • Button Firing too many times in Input System
  • Script adapted from Ruby's Adventure
  • Disabling Movement for UI
  1. UI OnlyFans

New Input System - Interact

PreviousChoices in DialogueNextDesigning the General Menu

Last updated 1 year ago

Reference Link:

Fixing Stupidity

The raycast will hit itself if you don't set up the project files correctly.

Reference Link:

Uncheck at least the "Queries Start in Colliders."

Testing

To make sure the raycast is not stupid, check it with:

float distance = Mathf.Abs(hit.point.y - rb.position.y);
Debug.Log(distance);
Debug.Log(lookDirection);

It's because I tested the distance that I was figuring out why I kept getting a hit each time.

Look Direction

You want to set this where you set that lastMoveX in the animator.

public void OnMove(InputAction.CallbackContext ctx)
{
    ...

    if (movementInput.x == 1 || movementInput.x == -1 || movementInput.y == 1 || movementInput.y == -1)
         ...
        lookDirection = new Vector2(movementInput.x, movementInput.y);
    }
}

Button Firing too many times in Input System

// Example Code
public void OnMyAction(InputAction.CallbackContext context)
{
    if (context.started) {
        Debug.Log("Action was started"); 
        }
    else if (context.performed) {
        Debug.Log("Action was performed"); 
        }
    else if (context.canceled){
        Debug.Log("Action was cancelled");
        }
}

For our project:

public void OnInteract(InputAction.CallbackContext ctx)
{
        if (ctx.started) {
            Debug.Log("DiaManager Should Open");
        }
}

Script adapted from Ruby's Adventure

RaycastHit2D hit = Physics2D.Raycast(rb.position + Vector2.up, lookDirection, 1.5f, LayerMask.GetMask("Interactibles"));
if (hit.collider != null)
{
    DiaTrigger character = hit.collider.GetComponent<DiaTrigger>();
    if (character != null)
    {
        character.Begin();
    }
}

Disabling Movement for UI

To switch to UI, you can change the Action Map. You need to set the default action map in the previous section first before switching.

This Interact function will trigger the DiaManager's "Show" function (This is the UI controller for the dialogue box). That is when we want to stop the player from moving.

using UnityEngine.InputSystem;

...

private PlayerInput playerInput;

//Initial State of the Game
private void Start() 
{
    //I've put the playerInput component on the Player GameObject, hence the tag
    playerInput = GameObject.FindWithTag("Player").GetComponent<PlayerInput>();
}

public void Show(...)
{
    Debug.Log("DiaManager Should Open");
    playerInput.SwitchCurrentActionMap("UI");
}

Reference Link:

Reference Link:

https://docs.unity3d.com/ScriptReference/Vector2-ctor.html
https://forum.unity.com/threads/player-input-component-triggering-events-multiple-times.851959/
https://learn.unity.com/tutorial/world-interactions-dialogue-raycast?uv=2020.3&projectId=5c6166dbedbc2a0021b1bc7c#
https://stackoverflow.com/questions/38191659/unity-physics2d-raycast-hits-itself