Unity Input System: How to Pair Keyboard AND Mouse to the same InputUser

Unity’s new Input System is cool but the documentation needs a lot of work. Most tutorials on the new Input System are super basic and rely on the PlayerInput and PlayerInputManager components to do the magic of syncing users and control schemes which won’t work if you’re doing more custom gameplay or multiplayer setups.

So digging deeper you may have found the InputUser.PerformPairingWithDevice(...) method which will allow you to manually create a InputUser paired with a device like a gamepad. Which works great, however what is not obvious is how you pair 2 devices with a UserInput, like with the Keyboard AND the Mouse!

Well, after days of googling and banging my head against the wall I finally found the answer in a semi-unrelated forum thread:

var controls = new MyControls();
var user = InputUser.PerformPairingWithDevice(Keyboard.current);
InputUser.PerformPairingWithDevice(Mouse.current, user: user); // this is the key, non-intuitive part
user.AssociateActionsWithUser(controls);
user.ActivateControlScheme("KeyboardMouse");
controls.Enable();

So you still do the initial instantiation/pairing with one of the devices (the Keyboard in this case) and then follow it up with another call to PerformPairingWithDevice() this time passing the Mouse and the user that was just created.

This feels unfinished to me. It seems an odd process to pair two devices like this. Instead I wish you could pass in an array of devices when first instantiating the InputUser.

Hope others that struggle with this are able to find the answer faster now! Here’s the thread I found the solution: https://forum.unity.com/threads/solved-can-the-new-input-system-be-used-without-the-player-input-component.856108/#post-5669128

Day as an indy dev

Pupper says Go

Wake up somewhere between 7:30am and 8:30am, usually due to dog jumping on the bed. A, not so subtle, hint that she wants to go out and then eat. So, I do those things. Short walk with the Bubble then make her and I breakfast. My breakfast is usually a breakfast shake, or some other fast/light meal, her’s is crunchies. We both seem to value high speed nutrition delivery.

So now it’s about 30 min after we woke up and it’s time to boot up the PC and get some work in. I’ll usually crank out some work until about 11:30am, as by this time pupper want to go out again and I’m ready for a break.

Types of work

The work itself is typically in some combination of 3 categories:
Planning – High level figuring out What needs to be done. This is the project management task and where the scrum-like “Stories” come from. The result of this type of work is having a short list of Actionable tasks in the form of: “The player should be able to acquire Equipment which has passive effects during combat”

Research – This is when I’m gathering information on How I’m going to implement the tasks defined during Planning. It involves looking up articles or examples of other implementations of similar mechanics. While this is typically a technical task, sometimes research involves looking at game play mechanics rather than programming patterns/architecture.

Development – This is the real meat of the work. It’s when I know What I’m going to do and How I’m going to do it, all that is left is to Do it. Music on, head down, hands slapping keyboard. It also includes play testing.

Rarely are these tasks so clear cut. There is a lot of cross over and when doing time tracking I do not care to switch timers from one to the other unless there is a clearly defined transition from one phase to another. It’s not important that I know exactly how long I Plan vs Dev. Not yet anyway. All I really care about is the aggregate number of work hours per day.

Afternoon Lull

After the 11:30am dog outing it’s lunch time and usually an extended break of youtube or some gaming. 2pm-2:30pm sometimes sees a return to development, but the afternoon can be a real turbulent time. Mental energy is low. I’m still trying to figure out a way to get more dev work in during the early afternoon. Instead I’ll try to be productive in other areas like exercising, grocery shopping or some manual labor projects around the house.

Evening Hodgepodge

Around 5pm friends start appearing in Discord and group gaming ensues. This can take me into dinner and beyond depending. Since I’m on the computer I sometimes continue to fiddle with the project while chatting or waiting for the next game to begin. I don’t track this development time as it’s usually non-focused and more just play testing, taking notes or making minor changes.

While I don’t track it, it’s still useful progress. These unfocused tasks tend to be tedious and require a lot of context switching (change a little code, try it out in game, change a little more, test again, and so on…). So doing them while also chatting with pals distracts from the tedium and I actually make progress where there otherwise wouldn’t be.

Wind down

That usually rounds out the day. By 9pm, if I’m being good, I’ll do some mediation and wind down for a hour or so before bed.

And that’s the high level view. Rarely is it so clear cut but in general that’s what a day is like. Typically, I log about 3-4 hours focused on development per day. While not much if compared to the assumed 8 hour work day, I still make pretty significant strides on a day to day basis. 2 Break days per week, where I’m “not allowed” to work or even think about the project. Break days are ideally non-sequential and one takes place in the middle of the week, lately that means Wed and Sunday are break days.

Summonable Tokens and AI

I have a cool addition to the game: Summonable Tokens. I made a Necromancer token and cards, one of which is Summon Skeleton, it creates a token at one of the grave mounds that are placed around the map. The summoned skeleton token is a “Lesser Token” which doesn’t work like a regular token on your team in that it can’t “play cards”. No no, a Lesser Token only acts at the end of your turn and does a specific action each time. In the skeleton’s case, it’s “Shamble” which is a short distance melee attack.

Summoning lesser tokens can work for the player too, so I’m thinking of making a Beast Master character who can summon different animals which will be Lesser Tokens and have different behaviors such as a Wolf that Lunges, a Hawk can like… dive I guess or a boar can Charge (move in a direction until it hits something.

This morning I got the card action “InvokeLesserToken” working. As in, being able to play a card that forces your Lesser token(s) to do something. For example, the card Hunger Of The Dead: It causes a Lesser Token to do 3 Melee moves. The player doesn’t choose the Lesser Token’s target or aim it’s shot, it simply forces the token to do it’s own thing.

AI Play Evaluation Helper

Over the last 2 days I also implemented a new method on CardActions called SpecialAiEvaluation() which returns a float that the AI uses to evaluate a play. It adds or subtracts from the play’s score, which it turn makes the card more/less likely to be played by the AI. This is useful because sometimes the AI needs help deciding when a card is worth playing. Sometimes the card is just very powerful but hard for the AI to systematically understand why. For example Summon Skeleton; it gets another token on the field but the AI currently doesn’t evaluate that. It only ever looks at paths and targets’ properties. So the Summon Skeleton action’s SpecialAiEvaluation() simply returns a large number, in this case 300, which is a higher score than any standard evaluation the AI does itself ensuring the card is selected to be played.

This SpecialAiEvaluation is in contrast to how the AI in Hearthstone does it’s evaluation. During a talk at GDC years ago the original AI guy on the Hearthstone team said that one goal of his AI was to not use any special, helper methods of evaluating plays. This is great as it means the AI is advanced enough to understand the value of a card’s effects without having to explicitly be told.

While it is possible that I devise such a robust system for my project, it is likely unnecessary with the time/effort required not being worth it, at least not now. In the current form I can be highly specific about how a particular CardAction is evaluated. Later, if a particular action (like summoning a token) proves to be something that happens often the action’s special evaluation can be converted into a standard evaluation step. It’s easy to imagine that the general scoring of adding a new token to your team is usually a good thing, but it’s just not better than performing a game winning attack, or saving a higher value token with a heal.

Kick, Punch, Eat Meat Dev Log #3

Big month in many ways, not just for KPEM but also just general life. First, I got a dog! Meet Bubble, she is a good girl.

Bubble loves walks, wind and belly rubs

Also, Oxygen Not Included came out of Early Access on Steam and I’ve been hooked. The magnitude of my addiction to ONI is unlike that of any game in a long time. Something about it just keeps me building for hours. Klei did a fantastic job with it and I’m eager to see where they take it next. Since it’s so similar to base builders like RimWorld and Dwarf Fortress I’d really like to see some kind of external visitors brought into the mix.

When not walking the dog or creating cooling loops I’ve been plucking away at KPEM. So what’s been happening:

Card Drag UX improvements / Refactor

Needed to do this for a while. Card dragging had become overly complex so I needed to give it an overhaul. Now when you drag cards you get a targeting arrow and when played they have a little hop animation as they go to the discard pile. Basically just trying to get it to work as much like Hearthstone as possible. It also works across PC and touch screens!

Objectives! Purpose!

There are now 4 Objective locations in the world, each one has a mini boss. The idea to these objectives is that if the player defeats the mini-bosses within they not only get a major reward (cards/equipment) but also the final boss will become easier.

There will be thematic details on this stuff later, but mechanically the objectives are added to the world and a powerful enemy is encountered at each one. The rewards and their effect on the final boss still needs to be coded.

Timer Deck!

This is a big new mechanic. There is now a deck of cards called the Timer Deck. It is full of special cards which have an effect on the round. Timer cards are revealed, one at a time, after the player’s Movement phase, some have immediate effects, like starting a combat, while others change some aspect of the round and stay in effect until a new Timer Card is revealed. A Timer card can make you start all combats with an extra card while it’s in play or they can make certain terrain tiles easier or harder to move through. Overall, they add variety to each round.

It’s not called a Timer Deck for nothing. Spread out evenly in the Timer Deck are cards that will increase the difficulty of the enemies you encounter. It will be advantageous for players to complete events which add cards to the Timer Deck and avoid situations that will waste their time because when the Timer Deck is depleted the Final Boss will emerge and the Final Fight will take place!

Play testing with this has been fun and the best part is that it piggy backs off of the existing card logic and code. Meaning the time/effort to add in this feature was minimal compared to the amount of game play options it added to the KPEM.

Phases and the new Prep Phase

Like before, this allows the player to make choices about what cards they want in their deck beyond just the combat purposes of a card.

The flow of the game is now broken up into Phases. The round starts with the Prep(aration) Phase. In the Prep phase, the player is shown 3 cards from their deck, they are given the option to select as many of these 3 cards as they wish to be played for this round. In this phase, the cards in your deck provide Steps (movement points) and sometimes other effects on the round. For example, the card Kick provides many Steps, while Eat Meat reduces steps slightly, costs 1 meat to use but heals the player.

Rough Prep UI. Lots of placeholder stuff. Card Step values are in the blue box. Since I’m at full health I choose not to play Eat Meat this round.

After the player has locked in their Prep cards for the round the Explore Phase begins. In this phase, the player moves their character on the map, spending Steps. When the character lands on a special tile its effects are resolved right away.

When the player runs out of Steps the Explore phase is over and the Timer Phase begins. The Timer Phase is a short phase in which a new Timer Card is revealed, its effects resolve and then a new round begins.

And that is the overall Game Loop!

Enemy Intent

Enemies now display the type of attack and the amount of damage they will do on their turn. This is similar to other deck builder games like Slay the Spire and Guild of Dungeoneering.

This one I’ve avoided doing for a LONG time. In fact I fully intended to not display the enemy intent, instead just having the enemy attack always be somewhat unpredictable. But I must concede that allowing the player to see the enemy intent adds a lot of interesting gameplay and I cannot deny it any longer.

This also opens up a lot in terms of damage mitigation cards so I’m looking forward to adding more cards, equipment and enemy attack patterns with this new mechanic in mind!

Ok that’s everything for this month! Time to walk the dog and see if my Dreckos need sheering!

Convert Unity UI Screen Space position to World position

All credit goes to tosiabunio over on this forum post for this great solution.

Using Unity’s Canvas UI is excellent when you want your UI elements to scale and position correctly across different devices and aspect ratios. Learning to work with the Canvas UI properly is a bit of a nightmare but that’s a different story.

My issue was that while I do want UI elements to reposition across different aspect ratios I also want some GameObjects to have animations that seemingly interact with the UI elements. For instance, I wanted the Deck and Discard piles (UI Elements in Screen Space) to have Cards (Game Objects in World Space) come in/out of them.

GameObject Cards in World Space coming out of UI element Images in Screen Space. Works even as the UI elements move due to aspect ratio changes.

The problem is that you cannot simply have your World Space objects get the transform position of the Screen Space UI objects, the positions will not translate properly and you’ll end up with your World Space objects flying off the screen. This is where Tosiabunio’s solution comes into play when you want to ” Translate anchored overlay UI element to world position. “

var screenToWorldPosition = Camera.main.ScreenToWorldPoint(rectTransform.transform.position);

Basically, all you have to do is use the ScreenToWorldPoint method and pass it the desired UI object’s transform.position. It’s a simple one liner but this solution had evaded me for months where I was doing all kinds of crazy hacks trying to get these two positioning systems to play nice together.

Tosiabunio’s post also mentions a way to convert in the opposite direction, World Space into Screen Space:

Translate game object position from the world to overlay position in order to place UI element near that object.

To which he suggests:

RectTransformUtility.WorldToScreenPoint(Camera.main, obj.transform.position)

I haven’t tried this yet but it might be handy for others. Also, user karma0413 points out that WorldToScreenPoint can be slightly off in some situations and offers a solution which involves performing position calculations off the scale factor. Again, I haven’t tried it yet as the original one liner works perfectly for my purposes.

Let me know if this helps you and if you have any other Unity Canvas issues that you need help resolving. I’ve been working with it a lot lately and perhaps I can help or write more about common issues people face.

Kick, Punch, Eat Meat Dev Log #2

So many big steps made, things are shaping up nicely. There are still lots of mechanics with uncertainties around them and a whole lot of UI/UX improvements needed. But I’m here to celebrate the victories over the last month.

A major help was a day spent brainstorming with a friend of mine, Kevin Zipper. We went to Gumbo in Brooklyn, a shared work space for a gaggle of game devs. While there we used a meeting room for a day and really hashed out some aspects of the game focused on enemy abilities and over-world movement mechanics. It turned out to be a very productive 6 hours of white boarding and fleshing out of ideas.

One of the big things we discussed was how to get the player’s deck to play a larger role in the other aspects of the game. The core idea is that “The Deck is your Character” but the game had become something where the deck was only used in combat. This didn’t sit right with me so we spent half the day exploring ways that the deck could effect the other aspects of the game such as movement and events.

See, one of the issues I've had is that I'm not creative in a bubble (nor am I entertaining when alone, hence why my writing is too bland). I need someone to form ideas with but this game has always been just me. This is a big part of the reason development has been so slow and why the game has taken on so many other forms over the years. One day I'll put together a post showing the many different forms of the game has taken.

Anyway, let’s get into some detail on what’s changed.

Proc-Gen Map

Using proc-gen for map biome placement. Big thanks to another pal, Brian McLendon, the proc-gen wiz, for doing this. He’s able to turn Perlin Noise into a Perlin Symphony (sorry, working on being funny). Specifically, the world now utilizes a seed to generate height maps then uses those to decide which biome to assign to a tile. Previously it was just randomly picking a biome per tile, this has made the maps much more comprehensible.

Event ScriptableObjects

The Game Event system got a major overhaul and now runs off ScriptableObjects rather than an XML doc. This means a nicer way to create and edit events while making the events more customizable. Also, I implemented Choice Requirements, so some choices can be locked off unless the character has a specific card, equipment or whatever (like the blue text choices in FTL).

Equipment ScriptableObjects

Equipment now also utilizes ScriptableObjects, mostly for assigning all the visual and text aspects of the item. The equipment’s effect is still dictated by a specific class for each item, in order to give them unique and powerful effects. One cool thing that equipment can do is add more cards to your deck while the item is equipped, very much like in Card Hunter. So expect to see equipment that has powerful passive effects balanced with adding some bogus cards to your deck. Oh and there is an equipment type called Mutations, but I’ll leave it at that for now.

Movement Phase

This is a big change that mostly came as a result of the big brainstorming session. The player’s deck now plays a role in player movement on the overworld. During the Move Phase, the player is dealt a few cards from their deck. They then get to choose a card to use as their movement card. The card chosen will determine how many spaces they can move and may also provide other effects either in or out of combat. For instance, perhaps during the Move Phase a player chooses to use the Bandage Item card. This card doesn’t provide many move points, but it does heal the player. Or maybe they use Simple Plan, providing a moderate amount of move points while also giving an extra card draw in combat if it’s encountered.

There are still some aspects of this new mechanic that may change as testing continues but it’s a major step towards having a game where “The Deck is your Character”

Side Note: The original idea was to have this movement phase be just like 1 round of combat, using the same UI. But upon implementing this I immediately didn't like it. Despair set in, but I went ahead with a different iteration which I really like and is now what is currently in the game.

Dual Purpose Cards

To go along with the Movement changes, cards have had to change too. Cards have to play 2 roles now, Combat and Movement, so can have distinct abilities in each context. When looking at the cards in your deck or when viewing cards being offered to you, you’ll see the card’s stats and effects for both phases, but when in the combat or movement phase you’ll only see the values and effects that relevant to that phase.

Enemy Controllers

Previously, enemies were entirely defined using ScriptableObjects and their attacks were reusable classes like “DamagePlayer” and “AddCardsToDiscard”. These classes were attached to the enemy’s ScriptableObject via an array, giving them a list of possible attacks from which they would choose 1 to use each turn. This was great for reusability but not great for customization nor did it allow for complex, multi-turn enemy behavior (like charging up an attack to use next turn).

But now that’s all changed and I’ve introduced the EnemyController. With this, enemies get a slew of new potential abilities. Charge-up attacks, fleeing from combat, explode on death and much more.

The Rest

There area also a whole bunch of more minor changes, bug fixes and some changes that aren’t too impressive to talk about but are big code architecture changes that allow for stuff that you’d expect in this type of strategy game. Like spawning new enemies into the current combat. Seems like something that should be easy, but it required a lot of refactoring to get the system able to spawn in enemies all willy-nilly.

Wolf Girl calling in more pups! Those red boxes on that cards are temporary!

So that’s the big stuff. The game is nearing “Feature Complete” and the next few weeks will be focusing on cleaning up the UI/UX mess that has arose from all the rapid prototyping. Looking forward to getting new art and animations from Butzbo soon.

Kick, Punch, Eat Meat Dev Log #1

It is my quest to complete Kick, Punch, Eat Meat and if fortune favors me, turn Robot Monkey Brain into a sustainable venture. Leave a comment if there is any other aspect of this game dev stuff that you are interested in keeping tabs on and I’ll include it in future logs.
Here is what has been happening over the past 4 weeks:

  • Implemented Equipment as playable cards
  • Conducted multiple play tests, gathered a lot of quality feedback (thank you, friends!)
  • Removed Equipment as playable cards
  • Implemented Equipment as items added into Slots on the character. They provide passive effects.
  • Changed the map to a Hex map
  • Implemented Line Of Sight on the map
  • Added Ruins to the map, locations which present the player with special Events
  • Implemented multiple outcomes to events with probabilities used for selecting the outcome of the player’s Choice
  • Recongigering of the Combat UI elements
  • Much balancing of existing Cards, Enemies and abilities
  • Many bug fixes and performance tweaks
  • Progression towards making Robot Monkey Brain into an LLC
  • Researched the Photon plugin for Unity (for future projects)
  • Addressed a performance issue with Instantiating too many cards every time the deck changes
  • Added a number of new Poison card mechanics
An early version of the map with Hexes and their various Biomes. Chests represent Ruins for now and Mountains block movement and line of sight.

Side Quest

Eat all the food in my house before buying new stuff. I’m allowed to buy food that’ll go with something I’ve got an am trying to use up. For example, I have a bunch of granola, so I’m allowed to buy yogurt to eat it with. At this point, I’m getting pretty low on food stuffs and these pickle, salt and coffee ground sandwiches aren’t the best.

Unity Remote 5 not working: Fix

If you are attempting to use Unity Remote 5 on an Android device attached to a PC (and perhaps Mac/Linux) but upon clicking Play in Unity you game goes not show up in Unity Remote then these tips might help:

First, disconnect your Android device from your PC and shut down Unity to ensure you’re starting from a clean slate. This walkthrough also assumes you’ve completed the steps found in Unity’s documentation: https://docs.unity3d.com/Manual/UnityRemote5.html

Enable Developer Mode and USB debugging (or re-enable it)

On your Android Device, enable Developer Mode

  1. Open Settings
  2. Scroll down to and click About <Device> (Mine says About Tablet)
  3. Scroll down to the Build Number
  4. click Build Number many times (after a few times, a message will pop up telling you how many more times to click)

Now that Developer Mode is active

  1. Go to Settings>Developer Options
  2. Ensure Developer Options is toggled On (at the top of the list)
  3. Optional: Enable Stay awake 
  4. Scroll down, under Debugging enable USB Debugging

Now you device should be ready.

  1. Plug in your Android device to your PC.
  2. Open Unity Remote 5 on your Android Device
  3. Open you Unity project
  4. Click Play

You should see you Unity project on your Android device. If not, try shutting down Unity and Unity Remote and restarting each.

Change Unity’s C# Template Code Style

The first thing I used to do every time I’d edit a new C# script in Unity is delete the comments and unused usings which are all included as part of the template. Going through that activity for every new script was bad enough, but on top of that the code style used in the template is pretty far off from what is considered standard for C#. Eventually, I decided it was time to stop my pre-scripting ritual and just make the default template into what I want. So to begin:

Navigate to the ScriptTemplates folder in your Unity install folder.

Route with a typical Windows install: C:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates

Open 81-C# Script-NewBehaviourScript.cs.txt in your text editor of choice.

From here you can edit the file’s layout however you choose. Mine looks like this:

using UnityEngine;

public class #SCRIPTNAME# : MonoBehaviour 
{
    private void Start() 
    {
        #NOTRIM#
    }
    
    private void Update()
    {
        #NOTRIM#
    }
}

Note: You may be unable to save/overwrite the template file if you did not open your text editor As Administrator. Don’t worry, you can just Save As to your Desktop and drag/drop the new version from the Desktop to the ScriptTemplates directory.

Event Delegation in Unity

What is Event Delegation?

“…a helper object, known as a delegate, is given the responsibility to execute a task for the delegator
– Delegation Pattern Wikipedia

Why use Event Delegation?

A good practice when working with Unity (or any framework) is to decouple your UI logic from your game logic. It’s fine to have the UI code reach down to the game logic layer and call methods directly on it but having the game logic layer directly call methods on the UI logic’s layer is, in general, a bad practice because doing so “marries” you to whatever UI you are currently using. So to get around this we can use C#’s Delegates and Events to fire off methods throughout our UI and even on the game logic layer.

How to use Event Delegation with Unity

All we have to do is:

  • create a static Event Manager class
    • Add delegates and events to this Event Manager
  • Listeners will add functions to the events
  • Delegators will fire off the events

The Manager: Defining events

public class EventManager {
    public delegate void EnemyHovered(int laneIndex, int targetRange);
    public static event EnemyHovered EnemyTargetedRequest;
    public static void EnemyTargeted(int laneIndex, int targetRange) {
        if(EnemyTargetedRequest != null) EnemyTargetedRequest(laneIndex, targetRange);
    }
}

In this example we create a delegate with the type EnemyHovered which takes the arguments laneIndex and targetRange. Then we make an event EnemyTargetRequest which is the event that the listeners will subscribe functions to. Finally, there is a static method that can be called by delegators when they want to fire off this event, triggering all the functions that are subscribed to it.

The Listener: Subscribing to the event

In another class we can add the following:

void Awake() {
    EventManager.EnemyTargetedRequest += DoSomething;
}

public void DoSomething(int targetedIndex, int targetRange){
    // do something
}

In this class we define some function called DoSomething that takes the same arguments as our delegate. In the Awake() method we add the function to the static class’s EnemyTargetedRequest event.

The Delegator: Firing off events

In yet another class we can do the following:

public void OnPointerEnter(PointerEventData eventData) {      
    EventManager.EnemyTargeted(_index, _targetCount);
}

Here we’re just using Unity’s OnPointerEnter event as the UI event that will trigger our EnemyTargeted event. Because our Listener class subscribed it’s DoSomething function to the EnemyTargetRequest event when SignalManager.EnemyTargeted is called it will fire off the DoSomething function!

So with this, you can keep all your events off in the static Event Manager, subscribe to them when you need an object to react to an event and fire off the events whenever you need to which allows you to keep UI and game logic separated. You could even have GameObjects subscribe to events and unsubscribe from them as they are created and destroyed which is very useful when these objects are created and destroyed dynamically.