if statement not working correctly with keyinput
okey sorry for my bit messy code but here it is
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Movement1 : MonoBehaviour
{
public float speedF = 5;
public float speedFF = 1.8F;
public float speedB = 1.8F;
public float TurnSpeed = 200;
public float JumpSpeed = 10;
public float i;
public bool runF;
public bool runB;
public bool fall;
public bool MayJump;
public bool startedCounting;
public bool teleported;
public bool Jump;
public bool death;
public Transform Player;
public Rigidbody PlayerRagdoll;
public Animator anim;
public int CollisionCount = 0;
public Slider slider;
public AudioSource walksound;
private Rigidbody rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
startedCounting = false;
death = false;
slider = Slider.FindObjectOfType<Slider>();
AudioSource walksound = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
walksound.Stop();
}
if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKey(KeyCode.W) && !walksound.isPlaying)
{
walksound.Play();
walksound.loop = true;
}
//if moving set run true
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W) && runB != true)
{
runF = true;
}
else
{
runF = false;
}
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S) && runF != true)
{
runB = true;
}
else
{
runB = false;
}
// if running play run animation
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W) && runB != true && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("run") && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
{
anim.Play("run", 0, 0f);
}
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S) && runF != true && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("back") && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
{
anim.Play("back", 0, 0f);
}
if (Input.GetKey(KeyCode.Space) && MayJump == true)
{
Jump = true;
}
else
{
if(CollisionCount != 0)
{
Jump = false;
}
}
slider.value -= Time.deltaTime;
// if not running play idle animation
if (Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKeyUp(KeyCode.DownArrow) || Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.S) && runB != true && runF != true)
{
anim.Play("idle", 0, 0f);
}
if (fall == false)
{
anim.Play("idle", 0, 0f);
fall = true;
}
if(CollisionCount == 0)
{
StartCoroutine("WaitSeconds");
}
else
{
startedCounting = false;
}
//if no collsion play falling animation
if (CollisionCount == 0 && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall") && startedCounting == true)
{
anim.Play("fall", 0, 0f);
}
if(CollisionCount > 0)
{
if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
{
fall = false;
}
}
if(GameObject.Find("Stick_Figure_T-pose ragdoll(Clone)"))
{
Destroy(gameObject);
}
if(slider.value == 0)
{
MayJump = false;
}
else
{
MayJump = true;
}
}
please try to ignore everything that doesn't have to do with animations. so my problem is that if I run with w a s d i run perfectly fine but when i try and run with the arrow keys my animation starts every frame as if
!this.anim.GetCurrentAnimatorStateInfo(0).IsName("run")
doesn't exist.
but i also put some sound in the walking called walking sound but if i walk with w a s d the sound starts every frame as if
!walksound.isPlaying
doesn't exist but when i run with the arrow keys the sound works perfectly and stops like it should. Now the strange thing is that I used || in every if statement that asks input like
if (Input.GetKeyUp(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
}
so I have no clue why my code doesn't work properly.
just when i posted this i saw a mistake in the sound code so the sound is working now but my animations played while moving with arrow keys still don't work
Answer by TBruce · Jul 29, 2016 at 06:14 PM
The first two if statements in your Update() statement are incorrect.
Try the modified Update() function below.
void Update()
{
bool upKeyReleased = false;
bool downKeyReleased = false;
bool leftKeyReleased = false; // not currently used
bool rightKeyReleased = false; // not currently used
bool upKeyPressed = false;
bool downKeyPressed = false;
bool leftKeyPressed = false; // not currently used
bool rightKeyPressed = false; // not currently used
bool jumpKeyPressed = false;
if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.UpArrow))
{
upKeyReleased = true;
}
else if (Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.DownArrow))
{
downKeyReleased = true;
}
else if (Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.LeftArrow))
{
leftKeyReleased = true;
}
else if (Input.GetKeyUp(KeyCode.D) || Input.GetKeyUp(KeyCode.RightArrow))
{
rightKeyReleased = true;
}
else if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
upKeyPressed = true;
}
else if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
downKeyPressed = true;
}
else if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
leftKeyPressed = true;
}
else if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
rightKeyPressed = true;
}
if (upKeyReleased)
{
walksound.Stop();
}
else if (upKeyPressed && (!walksound.isPlaying))
{
walksound.Play();
walksound.loop = true;
}
//if moving set run true
if (upKeyPressed && (!runB))
{
runF = true;
}
else
{
runF = false;
}
if (downKeyPressed && (!runF))
{
runB = true;
}
else
{
runB = false;
}
// if not running running foreward animation and not falling
if (upKeyPressed && !runB && !fall && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("run") && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
{
anim.Play("run", 0, 0f);
}
// if not running running backward animation and not falling
if (upKeyPressed && !runF && !fall && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("back") && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
{
anim.Play("back", 0, 0f);
}
if (jumpKeyPressed && MayJump == true)
{
Jump = true;
}
else
{
if(CollisionCount != 0)
{
Jump = false;
}
}
slider.value -= Time.deltaTime;
// if not running play idle animation
if ((upKeyReleased || downKeyReleased) && (!runB) && (!runF))
{
anim.Play("idle", 0, 0f);
}
if (!fall)
{
anim.Play("idle", 0, 0f);
fall = true;
}
if(CollisionCount == 0)
{
StartCoroutine("WaitSeconds");
}
else
{
startedCounting = false;
}
//if no collsion play falling animation
if (CollisionCount == 0 && !this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall") && startedCounting == true)
{
anim.Play("fall", 0, 0f);
}
if(CollisionCount > 0)
{
if (this.anim.GetCurrentAnimatorStateInfo(0).IsName("fall"))
{
fall = false;
}
}
if(GameObject.Find("Stick_Figure_T-pose ragdoll(Clone)"))
{
Destroy(gameObject);
}
if(slider.value == 0)
{
MayJump = false;
}
else
{
MayJump = true;
}
}
@$$anonymous$$avina well thank you very much it works now however I made my response so late because I had to fix a few things especially the animations en the jumping however I troubles hooted it fairly quick for a beginner (I'm program$$anonymous$$g for almost 2 months now so I see myself as a beginner). this is the end result
Code moved here for length reasons.
I moved all those booleans to the top and you forgot to make it possible to make those booleans false and the jumping variables got messed up but everything works perfectly now thank you very much!
Unless for some reason you need to use the outside the Update() function, these need to either be be local to the Update() function.
public bool up$$anonymous$$eyReleased = false;
public bool down$$anonymous$$eyReleased = false;
public bool left$$anonymous$$eyReleased = false; // not currently used
public bool right$$anonymous$$eyReleased = false; // not currently used
public bool up$$anonymous$$eyPressed = false;
public bool down$$anonymous$$eyPressed = false;
public bool left$$anonymous$$eyPressed = false; // not currently used
public bool right$$anonymous$$eyPressed = false; // not currently used
public bool jump$$anonymous$$eyPressed = false;
If you keep the variables above the way they are you still need to make sure that you reset them all to false at the top of the Update() function. Becaus if you donot the next time the Update function is entered previous values can be retained.
You can also place everything in a separate function and call it when first entering Update(), Like so
void Get$$anonymous$$eyInput()
{
// reset all flags
up$$anonymous$$eyReleased = false;
down$$anonymous$$eyReleased = false;
left$$anonymous$$eyReleased = false; // not currently used
right$$anonymous$$eyReleased = false; // not currently used
up$$anonymous$$eyPressed = false;
down$$anonymous$$eyPressed = false;
left$$anonymous$$eyPressed = false; // not currently used
right$$anonymous$$eyPressed = false; // not currently used
jump$$anonymous$$eyPressed = false;
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.W) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.UpArrow))
{
up$$anonymous$$eyReleased = true;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.S) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.DownArrow))
{
down$$anonymous$$eyReleased = true;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.LeftArrow))
{
left$$anonymous$$eyReleased = true;
}
else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.D) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.RightArrow))
{
right$$anonymous$$eyReleased = true;
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
{
up$$anonymous$$eyPressed = true;
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow))
{
down$$anonymous$$eyPressed = true;
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
{
left$$anonymous$$eyPressed = true;
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D) || Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
{
right$$anonymous$$eyPressed = true;
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
jump$$anonymous$$eyPressed = true;
}
}
and inside Update just call
Get$$anonymous$$eyInput();
to be sure you'll see my "answer" I comment also on this post
Your answer
Follow this Question
Related Questions
Warning: Noob question imminent... 0 Answers
Oculus UI click firing previously clicked UI button before current clicked UI button 1 Answer
UNITY no longer detecting XBOX360 controller input,XBOX 360 controller no longer working with UNITY 0 Answers
Can't disable Mouse wheel's navigation actions,Can't disable Mouse wheel's navigation 0 Answers
Using Nintendo Switch pro controller 0 Answers