- Home /
Player Transformation
While working with the character controls, I noticed a problem. When two keys that translate the player in opposite directions are pressed at the same time, the player does not move. If one is already being held and the other is pressed, the player stops.
How can I get the player to continue in the same direction the player was originally moving before the second key is pressed, but not move when both are pressed at once and the player is not moving?
This is the code.
function Start () {
animation["F1WalkForward"].layer = 2;
animation["F1WalkBack"].layer = 2;
animation["F1WalkLeft"].layer = 1;
animation["F1WalkRight"].layer = 1;
}
var FootStepSound : AudioClip;
var JumpSound : AudioClip;
var MoveAmount = 0.15;
var JumpAmount = 250;
function Update () {
if (Input.GetKey(KeyCode.W)) {
transform.Translate (Vector3.forward * MoveAmount);
audio.clip = FootStepSound;
audio.Play();
animation.Play("F1WalkForward");
}
if (Input.GetKey(KeyCode.S)) {
transform.Translate (Vector3.back * MoveAmount);
audio.clip = FootStepSound;
audio.Play();
animation.Play("F1WalkBack");
}
if (Input.GetKey(KeyCode.A)) {
transform.Translate (Vector3.left * MoveAmount);
audio.clip = FootStepSound;
audio.Play();
animation.Play("F1WalkLeft");
animation.Stop("F1WalkForward");
animation.Stop("F1WalkBack");
}
if (Input.GetKey(KeyCode.D)) {
transform.Translate (Vector3.right * MoveAmount);
audio.clip = FootStepSound;
audio.Play();
animation.Play("F1WalkRight");
animation.Stop("F1WalkForward");
animation.Stop("F1WalkBack");
}
}
For example, when the A & D keys are pressed at the same time, I do not want the player to move. When the A key is currently being held and the D key is pressed, I do not want the player to stop translating in the direction of the A key.
Also, how can I loop the foot steps sound only while one of the keys are being held?
Answer by CaioRosisca · Sep 28, 2012 at 06:37 PM
Unfortunatelly I can´t test it here, but that should work. Your if() should be like that:
if (Input.GetKey(KeyCode.W)) {
isForward = true;
if(!isBack){//!isBack is the same as isBack == false
transform.Translate (Vector3.forward * MoveAmount);
}
}
else {
isForward = false;
}
if (Input.GetKey(KeyCode.S)) {
isBack = true;
if(!isForward){
transform.Translate (Vector3.back * MoveAmount);
}
}
else {
isBack = false;
}
Try it again using this piece of code.
@Coreyf716 if your question is solved, please upVote my answer and mark it as the correct one :)
Answer by CaioRosisca · Sep 27, 2012 at 12:06 AM
For me that´s exactly how it should work and I see absolutely no reason for doing this, but each case is a case. So here we go, what you could try is, inside the if() that moves the player to the right you could also check if the player wasn´t already moving left(), if it was, simply return.
For example:
if (Input.GetKey(KeyCode.D)) {
if(Input.GetKey(KeyCode.A))
return;
transform.Translate (Vector3.right * MoveAmount);
audio.clip = FootStepSound;
audio.Play();
animation.Play("F1WalkRight");
animation.Stop("F1WalkForward");
animation.Stop("F1WalkBack");
}
Then do this for each key.
Hmm, you could try to set booleans for each move direction, like isLeft, isRight..when u pless the relative key set them to true, and when moving to the oposite direction try to do the same as above, but with an else..inside if(key.D) put is(isLeft){return} else {all the moving right code}..\
make sure that when you are not pressing the key the boolean is set back to false
I'm using this script.
function Start () {
}
var isForward : boolean = false;
var isBack : boolean = false;
var is Left : boolean = false;
var isRight : boolean = false;
var $$anonymous$$oveAmount = 0.15;
function Update () {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)) {
transform.Translate (Vector3.forward * $$anonymous$$oveAmount);
isForward = true;
}
else {
isForward = false;
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S)) {
transform.Translate (Vector3.back * $$anonymous$$oveAmount);
isBack = true;
}
else {
isBack = false;
}
}
}
This way, isForward is only active while W is being held. I've tried if (isForward == true) {
} but it didn't work. I also tried if(isRight == true && isLeft == true) {
} but when this statement is attached to both keys, it does the same as it would without the statement.
Is it possible to cancel a statement if another key is being held?
Your answer
Follow this Question
Related Questions
Is it possible to continue a method after Input? 2 Answers
Moving Animations 3 Answers
Transform.Translate change distance and speed 1 Answer
How to translate object one movement with script 1 Answer
Rotating An Object On Its Axis Once 3 Answers