Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

PowerQuest

A Nifty 2D Adventure Toolkit for Unity · By Powerhoof

Wasd keyboard movement

A topic by SebastianL created Apr 28, 2024 Views: 102 Replies: 1
Viewing posts 1 to 2

Is it possible, and how to implement it? :)

(+1)

Yeah, not too hard if you're alright with some scripting.  I do it by simulating a mouse click so it can still be interrupted by "OnWalkTo" scripts, etc.

This code works in the UpdateInput function (if you have that in your global script, otherwise regular Update function is fine):

// First check if we should be able to move
if ( E.Paused == false && E.GameHasKeyboardFocus && E.GetBlocked() == false && Plr.Moveable )
{
     // Get direction from keyboard input
     Vector2 direction = Vector2.zero;
     if ( Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow) )
         direction += Vector2.up;
     if ( Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow) )
         direction += Vector2.down;
     if ( Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) )
         direction += Vector2.left;
     if ( Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow) )
         direction += Vector2.right;
      // Start moving 2 units in that direction
     if ( direction.sqrMagnitude > 0 )
     {
         Vector2 targetPos = Plr.Position + (direction.normalized * 2.0f);
         E.ProcessClick(eQuestVerb.Walk,null,targetPos);
     }
}