Okay, I made a code for moving my character, it’s pretty simple, using the Input.GetKey. the thing is, i’m using the arrow keys, and when i press both right and left, i wanted my character to stop. So i made an if condition where, if both keys where pressed, nothing happens.
now with up and down, the same thing, if both are pressed, dont enter the code. I thought it was alright, but now, if I press both side arrows, and press up, nothing happens, but if I press down, it goes down, and even, if i press up WHILE pressing down, and then release the down key, it continue to goes down, even if I’m pressing up. (the same goes to diseways, with right).
So, does having multiple inputs checks generates problem for the unity api or something, or is my code just bad?
here is it:
using UnityEngine;
using System.Collections;
public class SoulsControl : MonoBehaviour
{
public float Speed;
private GameObject Soul;
private Vector3 Acceleration;
public bool Movement;
public bool CanInput;
public int ButSelection;
public GameObject[] Buttons;
private Vector3 ButPosition;
private Vector3 SelectorPosition;
// Use this for initialization
void Start()
{
Soul = this.gameObject;
Movement = false;
ButSelection = 0;
UpdateSelectorPos();
}
// Update is called once per frame
void Update()
{
if (CanInput)
{
if (Movement)
{
Move();
}
else
{
Select();
}
}
}
void MoveX()
{
if (!(Input.GetKey(KeyCode.LeftArrow) & Input.GetKey(KeyCode.RightArrow)))
{
if (Input.GetKey(KeyCode.LeftArrow))
{
Acceleration.x = -Speed * Time.deltaTime;
print("IsLeft");
}
if (Input.GetKey(KeyCode.RightArrow))
{
Acceleration.x = Speed * Time.deltaTime;
print("IsRIGHT");
}
print("X");
}
}
void MoveY()
{
if (!(Input.GetKey(KeyCode.UpArrow) & Input.GetKey(KeyCode.DownArrow)))
{
if (Input.GetKey(KeyCode.UpArrow))
{
Acceleration.y = Speed * Time.deltaTime;
print("IsUP");
}
if (Input.GetKey(KeyCode.DownArrow))
{
Acceleration.y = -Speed * Time.deltaTime;
print("IsDOWN");
}
print("Y");
}
}
void Move(){
MoveX();
MoveY();
Soul.transform.position += Acceleration;
Acceleration = new Vector3(0, 0, 0);
}
void Select()
{
if (Input.GetKeyDown(KeyCode.LeftArrow)){
if (ButSelection > 0){
ButSelection--;
UpdateSelectorPos();
}
}
if (Input.GetKeyDown(KeyCode.RightArrow)){
if(ButSelection < 3){
ButSelection++;
UpdateSelectorPos();
}
}
}
void UpdateSelectorPos(){
ButPosition = Buttons[ButSelection].transform.position;
SelectorPosition = ButPosition;
SelectorPosition.x -= 1.95f;
SelectorPosition.y -= 0.05f;
Soul.transform.position = SelectorPosition;
}
}
pls ignore the Select function, and the UpdateSelectorpos, or anything to do with selecting stuff.
and note that i left some prints in the MoveX and Move Y functions. there are prints for every arrow, and the X and Y should not appear if you are pressing both keys on that axis.
If you are pressing all the arrow keys, X and Y should stop printing, but they don’t, after one has stopped, the other do not stop. and thats the problem.
also, CanInput and Movement are both bool that are chenged by others scripts.
thanks for any help.
*Sorry for bad english.