- Home /
Player can't move left or right while holding W and S at the same time - No code making it do that?
Here's my code. Never did I command it to not animate, let alone stop allowing me to move left and right completely, when holding W and S. I can, however, hold A and D and move up and down…
UPDATE It doesn't even recognize when I'm pressing A or D under said circumstances.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float Speed;
public Animator AnimatorGet;
private bool Up;
private bool Left;
private bool Down;
private bool Right;
private bool UpDown;
private bool LeftRight;
void FixedUpdate () {
// Movement Controls and Animation Initiation
if (Input.GetKey (KeyCode.W))
{
transform.position += Vector3.up * Speed * Time.deltaTime;
Up = true;
}
else
{
Up = false;
}
if (Input.GetKey (KeyCode.A))
{
transform.position += Vector3.left * Speed * Time.deltaTime;
Left = true;
}
else
{
Left = false;
}
if (Input.GetKey (KeyCode.S))
{
Down = true;
transform.position += Vector3.down * Speed * Time.deltaTime;
}
else
{
Down = false;
}
if (Input.GetKey (KeyCode.D))
{
Right = true;
transform.position += Vector3.right * Speed * Time.deltaTime;
}
else
{
Right = false;
}
Answer by NoseKills · Mar 21, 2015 at 02:27 AM
That's an awful lot of redundant if's.
It's really hard to figure out what EXACTLY you want to do by looking at code that doesn't do that. But I tried to guess...
Here's your code simplified
public float Speed;
public Animator AnimatorGet;
private bool Up;
private bool Left;
private bool Down;
private bool Right;
private bool UpDown;
private bool LeftRight;
void FixedUpdate () {
Up = Input.GetKey(KeyCode.W);
Left = Input.GetKey(KeyCode.A);
Down = Input.GetKey(KeyCode.S);
Right = Input.GetKey(KeyCode.D);
bool isWalking = false;
if (Up != Down) // if only up or down is pressed...
{
// ...we know we are walking
isWalking = true;
if (Up) // up
{
transform.position += Vector3.up * Speed * Time.deltaTime;
}
else if (Down) // or down
{
transform.position += Vector3.down * Speed * Time.deltaTime;
}
}
if (Left != Right) // same with left & right
{
isWalking = true;
if (Left)
{
transform.position += Vector3.left * Speed * Time.deltaTime;
}
else if (Right)
{
transform.position += Vector3.right * Speed * Time.deltaTime;
}
}
AnimatorGet.SetBool ("Is Walking", isWalking);
bool anyUp = Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.D);
bool anyDown = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D);
if (anyUp || anyDown) // if any movement key was released or pressed...
{
// ...change direction to current movement direction
if (Up != Down)
{
if (Up)
{
AnimatorGet.SetInteger ("Direction", 1);
}
else if (Down)
{
AnimatorGet.SetInteger ("Direction", 3);
}
}
// no diagonal directions.
// If we move Up and Left, direction is "4" because this Left/Right if is after Up/Down if
if (Left != Right)
{
if (Left)
{
AnimatorGet.SetInteger ("Direction", 4);
}
else if (Right)
{
AnimatorGet.SetInteger ("Direction", 2);
}
}
}
}
Thank you, Ill try it right now.. While i have you, I noticed its not firing Get$$anonymous$$ey for A and D while holding W and S at the same time. Why would that be exactly?
I'm just using a 3D cube to test this and I had to replace the animation calls with Debug.Log("Is walking false");
etc. because I don't have an animation I could use to see what's happening.
For me the movement works ok. If i hold W & S, the cube doesn't move but if I press A or D on top of those, it moves sideways. I guess it might even be a limitation of your keyboard but the limit shouldn't be this low. Hard to say.
Okay i tried the script… I even took out all the animation to see if it was the problem (obviously not) and even this is not working.
using UnityEngine;
using System.Collections;
public class PlayerController : $$anonymous$$onoBehaviour {
public float Speed;
public Animator AnimatorGet;
private bool Up;
private bool Left;
private bool Down;
private bool Right;
void FixedUpdate () {
Up = Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W);
Left = Input.Get$$anonymous$$ey ($$anonymous$$eyCode.A);
Down = Input.Get$$anonymous$$ey ($$anonymous$$eyCode.S);
Right = Input.Get$$anonymous$$ey ($$anonymous$$eyCode.D);
if (Up == true && Down == false)
{
transform.position += Vector3.up * Speed * Time.deltaTime;
}
if (Left == true && Right == false)
{
transform.position += Vector3.left * Speed * Time.deltaTime;
}
if (Down == true && Up == false)
{
transform.position += Vector3.down * Speed * Time.deltaTime;
}
if (Right == true && Left == false)
{
transform.position += Vector3.right * Speed * Time.deltaTime;
}
if (Up == true || Left == true || Down == true || Right == true)
{
if (Up != Down && Left != Right)
{
AnimatorGet.SetBool ("Is Walking", true);
}
}
}
}
Yeah is it because i have an OSX keyboard? and a mac? how can i make the key memory bigger?
Answer by fredlllll · Mar 21, 2015 at 07:42 AM
You might experience a phenomenom called "ghosting" (http://www.microsoft.com/appliedsciences/antighostingexplained.mspx ). it means some keyboards cant recognize more than 2 keys pressed. most computers will greet you with a warning sound when ghosting occurs. if i press w and s and then d i get a beep.
you might add some debug output to output the values your input gives you (some Debug.Log("w: ..... which then outputs "w:true s:true a:false d:false" so you can make sure that the key isnt registered (oh i just saw you already checked and a and d are not recognized)
also use the normal update for input handling http://stackoverflow.com/questions/19259097/is-it-really-wrong-to-use-input-getkey-on-fixedupdate
Yeah I just got a keyboard with 10-key rollover. Its a razer blackwidow chroma :)