Hello everybody,
I would like to use the d-pad up and down of my Xbox one controller like buttons, so that they fire only the first frame as long as they are pressed.
Although this seems to have been discussed a lot of times already I can not get this to run properly. The cube still moves in X / Y direction as long as any of the buttons is pressed.
Does anyone spot my error or have a solution to this?
public class test_move : MonoBehaviour {
public float p1_horizontal; // save d-pad values
public float p1_vertical; // save d-pad values
public bool p1_up = false;
public bool p1_down = false;
public bool p1_left = false;
public bool p1_right = false;
public bool p1_horizontal_pressed = false;
public bool p1_vertical_pressed = false;
public float speed = 11f;
void Update () {
p1_horizontal = Input.GetAxisRaw("p1_horizontal");
if (p1_horizontal < 0) {
p1_left = true;
p1_right = false;
} else if (p1_horizontal > 0) {
p1_left = false;
p1_right = true;
} else {
p1_left = false;
p1_right = false;
}
p1_vertical = Input.GetAxisRaw("p1_vertical");
if (p1_vertical > 0 && p1_vertical_pressed == false) {
p1_vertical_pressed = true;
p1_up = true;
p1_down = false;
} else if (p1_vertical < 0 && p1_vertical_pressed == false) {
p1_vertical_pressed = true;
p1_up = false;
p1_down = true;
} else {
p1_vertical_pressed = false;
p1_up = false;
p1_down = false;
}
transform.Translate(p1_horizontal * Time.deltaTime * speed, p1_vertical * Time.deltaTime * speed, 0);
}
}
Try to make it like this:
bool upIsPressed = false;
bool downIsPressed = false;
bool isNeutral = true;
void Update () {
float horizontal = Input.GetAxis("P1_Horizontal");
if (horizontal < 0 && !downIsPressed)
{
isNeutral = false;
downIsPressed = true;
YourDownFunction();
}else if (horizontal > 0 && !upIsPressed)
{
isNeutral = false;
upIsPressed = true;
YourUpFunction();
}
else if (horizontal == 0 && !isNeutral)
{
isNeutral = true;
downIsPressed = false;
upIsPressed = false;
}
This is just one Horizontal but you can do it just like that for vertical.
OH my goddness I just realized I am using the Input.GetAxisRaw for the translate directly, so no wonder it is exeuted every frame regardless everything else facepalm
@Khazmadu thanks for the hint with doing the translate in a separate function 
I got to rework this when I am back home and post the working code here.
Also notice that @Khazmadu 's script has an else-if in the last part, which is a crucial fix. Without it the translation would happen every other frame.
Finally got it running (I should not do such things after 11pm, I kept on mixing up variables).
Anyhow here goes my odd target to move something with a d-pad smoothely left and right but only in steps up and down
public float p1_horizontal; // save d-pad values
public float p1_vertical; // save d-pad values
public bool leftIsPressed = false;
public bool rightIsPressed = false;
public bool vertNeutral = true;
public bool upIsPressed = false;
public bool downIsPressed = false;
public float speed = 11f;
void Update() {
p1_horizontal = Input.GetAxis("p1_horizontal");
if (p1_horizontal < 0) {
leftIsPressed = true;
p1_horiz_move();
} else if (p1_horizontal > 0) {
rightIsPressed = true;
p1_horiz_move();
} else if (p1_horizontal == 0) {
leftIsPressed = false;
rightIsPressed = false;
}
p1_vertical = Input.GetAxisRaw("p1_vertical");
if (p1_vertical < 0 && !downIsPressed) {
vertNeutral = false;
downIsPressed = true;
p1_vert_move();
} else if (p1_vertical > 0 && !upIsPressed) {
vertNeutral = false;
upIsPressed = true;
p1_vert_move();
} else if (p1_vertical == 0 && !vertNeutral) {
vertNeutral = true;
downIsPressed = false;
upIsPressed = false;
}
}
void p1_horiz_move() {
transform.Translate(Vector3.right * p1_horizontal * Time.deltaTime * speed);
}
void p1_vert_move() {
transform.Translate(Vector3.up * p1_vertical * Time.deltaTime * speed);
}