WASD/Arrow key movement
I have looked around on the forums, and I can't seem to find a script that allows a 2D sprite to be moved with WASD or the arrow keys. I'm rather new to Unity, can someone please share a script with me or point me in the right direction? This is the script I am currently using`using UnityEngine; using System.Collections;
public class Ctrl : MonoBehaviour { public float speed = 1.5f;
void Update ()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += Vector3.up * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += Vector3.down * speed * Time.deltaTime;
}
}
}`
I don't know about for 2D, but in 3D I've been using the new inputs feature with callbacks instead of listening for keycodes manually.
With the new system, you create an action and assign as many bindings to it as you want. Then, when one of those bindings is triggered a specific function in your script can be triggered.
Pseudo example:
Action: $$anonymous$$ovement
Bindings: Left Stick, WASD, Arrow_Keys
Callback Function: YourClass.$$anonymous$$ove()
Pseudo code of your class:
public void $$anonymous$$ove(InputAction.CallbackContext context)
{
moveVector = context.ReadValue<Vector2>();
if(moveVector.magnitude != 0)
{
// Do your movement stuff here
}
else
{
// Do your end walking stuff here
}
}
That basically says:
Save the button press info to a Vector2
If the Vector2 is not (0,0) do some movement stuff
If the Vector2 is (0,0) do some other stuff that would likely be the character co$$anonymous$$g to a stop
context contains all kind of useful stuff, not just is a button pressed. In the example context.ReadValue() says to read the x,y coordinates of that action where x and y can be -1, 0, 1 (or any float in between). This lets you know if you are pressing a button to walk forward, backward, left, right, or some combination.
To use the new input system, you'll need to install the package and enable it: Input System Installation
Once you have it installed, here's the quick start guide: Inputs Quick Start
I know listening for keys and acting on them is probably more straight forward than this, but this has the benefit of updating keybindings much simpler in my opinion.
Answer by KBCreater · Jun 14, 2017 at 03:40 AM
@Vandrcommander This may or may not help
public float panSpeed = 20f;
void Update () {
Vector3 pos = transform.position;
if (Input.GetKey ("w")) {
pos.z += speed * Time.deltaTime;
}
if (Input.GetKey ("s")) {
pos.z -= speed * Time.deltaTime;
}
if (Input.GetKey ("d")) {
pos.x += speed * Time.deltaTime;
}
if (Input.GetKey ("a")) {
pos.x -= speed * Time.deltaTime;
}
transform.position = pos;
}
You would have to use wasd in this version and is usually standard in most games. I could not fix the arrow key problem even after trying. Some one else would have to help you with that. Just drop the code into the game object and you should be good.