- Home /
Why isn't my character moving left/right or rotating!
I'm creating a temple-run-like game as a prototype and this is the update() script for it:
if (PlayerAlive) {
/*
* While the player is alive, move at velocities dictated by
* lane changing (x-velocity) - A/D Left/Right
* jumping (y-velocity) - W Up --- jumping is not implemented yet
* forward constant (z-velocity)
*/
transform.Translate(0f, 0f, this.currentZVelocity * Time.deltaTime, Space.Self);
/*
* Handle key presses, assigned respectiveley as mentioned prior.
*/
if (Input.GetKeyDown(KeyCode.A | KeyCode.LeftArrow)) {
if (Mathf.Approximately(transform.position.x, 0f) ||
Mathf.Approximately(transform.position.x, 2f)) {
transform.Translate(-2f, 0f, 0f, Space.Self);
}
}
else if (Input.GetKeyDown(KeyCode.D | KeyCode.RightArrow)) {
if (Mathf.Approximately(transform.position.x, 0f) ||
Mathf.Approximately(transform.position.x, -2f)) {
transform.Translate(2f, 0f, 0f, Space.Self);
}
}
else if (Input.GetKeyDown(KeyCode.S | KeyCode.DownArrow)) {
Slide = true;
}
/*
* Apply asthetic rotation and translation for sliding.
*/
if (Slide && !Sliding) {
transform.rotation = new Quaternion(-80f, 0f, 0f, transform.rotation.w);
transform.position = new Vector3(transform.position.x, 0.5f, transform.position.z);
Camera.main.transform.rotation = new Quaternion(80f, 0f, 0f, Camera.main.transform.rotation.w);
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, 0.5f, Camera.main.transform.position.z);
Sliding = true;
} else if (Slide && Sliding && SlidingTimeDelta < SlidingTime) {
SlidingTimeDelta += Time.deltaTime;
} else if (Slide && (Mathf.Approximately(SlidingTimeDelta, SlidingTime) || SlidingTimeDelta > SlidingTime)) {
transform.rotation = new Quaternion(0f, 0f, 0f, transform.rotation.w);
transform.position = new Vector3(transform.position.x, 1.5f, transform.position.z);
Camera.main.transform.rotation = new Quaternion(0f, 0f, 0f, Camera.main.transform.rotation.w);
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, 4f, Camera.main.transform.position.z);
Sliding = false;
Slide = false;
SlidingTimeDelta = 0f;
}
}
The only part of the code that seems to be functioning correctly is the transform.Translate(0f, 0f, this.currentZVelocity * Time.deltaTime, Space.Self);
part.
When i press A or D, nothing new happens. Same with if i press S.
However, i have noticed that the x-axis on my character is slowly increasing from 0f. I can imagine this is because of the inaccuracy of floats and i am constantly translating the character to x = 0f relative to Space.Self
... This seems easy to fix, though.
So, why is it that my script isn't acting whenever i press A, D or S?
Answer by robertbu · Apr 13, 2014 at 09:56 PM
The likely cause of your problem is that you are treating KeyCodes as if they are bits that can be combined. You need to change this:
if (Input.GetKeyDown(KeyCode.A | KeyCode.LeftArrow)) {
To:
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) {
I also think the rest of the code inside the clause may have problems. Try something like this instead:
if ((Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) {
if (transform.position.x > -0.5f) {
Vector3 pos = transform.position;
pos.x = -2.0f;
transform.position = pos;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613142636im_/https://answers.unity.com/themes/thub/images/avi.jpg)