- Home /
Moving in one direction at the time
Hello.
I would like to move my character only in one direction at the time (only left, or only top etc). It works correctly but the problem is that when I move horizontally I can go up and down (by pressing W or S) while still pressing A or D (which is OK), but when I move vertically I have to stop pressing W or S to move left or right. I know why this is happening, but I don't know how to fix that. Here is my code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
//Public variable to change speed of the player
public float MovementSpeed = 20f;
void Start () {
}
void Update () {
float forwardSpeed = Input.GetAxis("Vertical") * MovementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * MovementSpeed;
//This variables will be used to check whether player is pressing keys assigned to Horizontal and Vertical
bool statusHorizontal = Input.GetButton("Horizontal");
bool statusVertical = Input.GetButton("Vertical");
//By default character is not moving
Vector3 speed = new Vector3 (0, 0, 0);
//This is stupid, but in original Tanks 1990 player was able to move forward OR to the side
if (statusVertical == true)
{
speed.z += forwardSpeed;
}
else if (statusHorizontal == true)
{
speed.x += sideSpeed;
}
CharacterController cc = GetComponent<CharacterController> ();
cc.SimpleMove (speed);
}
}
Yes, I'm going something a 'la Tanks 1990 to learn Unity.
~Laran
Answer by robertbu · Nov 28, 2013 at 08:06 PM
Figure out this kind of logic can get ugly. I think this is what you are trying to do:
using UnityEngine;
using System.Collections;
public class PlayerController1 : MonoBehaviour {
//Public variable to change speed of the player
public float MovementSpeed = 20f;
private bool prevStatusVertical;
private bool prevStatusHorizontal;
private bool moveHorizontal;
private CharacterController cc;
void Start () {
cc = GetComponent<CharacterController> ();
}
void Update () {
float forwardSpeed = Input.GetAxis("Vertical") * MovementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * MovementSpeed;
//This variables will be used to check whether player is pressing keys assigned to Horizontal and Vertical
bool statusHorizontal = Input.GetButton("Horizontal");
bool statusVertical = Input.GetButton("Vertical");
if (statusHorizontal && !prevStatusHorizontal)
moveHorizontal = true;
if (statusVertical && !prevStatusVertical || !statusHorizontal)
moveHorizontal = false;
//By default character is not moving
Vector3 speed = new Vector3 (0, 0, 0);
if (statusVertical && !moveHorizontal)
{
speed.z += forwardSpeed;
}
else if (statusHorizontal)
{
speed.x += sideSpeed;
}
prevStatusVertical = statusVertical;
prevStatusHorizontal = statusHorizontal;
cc.SimpleMove (speed);
}
}
Thank you, but this still don't satisfy me. Problem is that when I press A or D (Horizontal), then W or S (Vertical), then release Vertical (but still keep pressing Horizontal) my character is NOT moving. It works however when I "press Vertical > press Horizontal > release Horizontal"
Tell me if I explained this badly:P
"$$anonymous$$e no Englando" :P
I added a $$anonymous$$or tweak that fixes the issue you outline.
Thank you, but I have one more (silly) question.
In line 29 there is: "If variable1 is true and variable2 (which is equal to variable1) is false, do something"
I don't see any logic in this. For me it's like "if sun shines and sun does not shine, do something..."
Take a close look at the variable names. On is 'statusHorizontal' the second one is 'prevStatusHorizontal'. This code is testing for the change is state of statusHorizontal. So if in the previous frame statusHorizontal was false, and in the current frame it is true, then we set 'moveHorizontal' to true.
Oh wait... I completely forgot that this whole code is executed in every frame (so prevStatusVertical has value of previous frame, not current).
This make sense now. You are great! (and I have a lot to learn :P)
Your answer
Follow this Question
Related Questions
C# -- Build character unit from script 1 Answer
C# Input.GetKey("Tab") Double Tap 1 Answer
C# Throw GameObject Upon Mouse Click 1 Answer
Player lives script help 1 Answer
Null in GetValidMethodInfo 0 Answers