- Home /
My movement keys are opposite to actual movement?
For background, im doing the stealth tutorial, player movement. Ive followed the instructions, but when I came to test it, the movements are backwards. (eg W/up moves down, A/left moves right etc) Weirdly, when I sneak, the movement is the right way round.
Any ideas where I have gone wrong? If I cannot find what went wrong, should I just try to add something that inverts the keys?
Edited for clarity. So here are my issues:
My code seems to be good
My input manager settings seem to be good
My keyboard seems to be good
I face the correct direction, but I move backwards.
During Sneak, I face the correct direction and move forwards.
If I invert using the input manager I face the wrong direction and move correctly, and in sneak I face the wrong direction and move backwards.(basically doing what invert should do)
The web player seems to be working fine, and after I go through the build settings, things work well with no problems.
Both my version and the done version have this problem, but another project(roll a ball) does not.
Your model sounds like it is aligned in the 3D software 180 degrees to Unity world axes. Is there rotation in the transform?
No, there is no rotation for the player character.
Did you simply put your $$anonymous$$uses in the wrong place? -x ins$$anonymous$$d of x for example in your movement scripts?
I dont have any $$anonymous$$uses in my scripts afaik
here is my code:
using UnityEngine; using System.Collections;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour { public AudioClip shoutingClip; public float turnSmoothing = 15f; public float speedDampTime = 0.1f;
private Animator anim;
private HashIDs hash;
void Awake()
{
anim = GetComponent<Animator>();
hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
anim.SetLayerWeight(1, 1f);
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
bool sneak = Input.GetButton("Sneak");
$$anonymous$$ovement$$anonymous$$anagement(h, v, sneak);
}
void Update()
{
bool shout = Input.GetButtonDown("Attract");
anim.SetBool(hash.shoutingBool, shout);
Audio$$anonymous$$anagement(shout);
}
void $$anonymous$$ovement$$anonymous$$anagement(float horizontal, float vertical, bool sneaking)
{
anim.SetBool(hash.sneakingBool, sneaking);
if(horizontal != 0f || vertical != 0f)
{
Rotating(horizontal, vertical);
anim.SetFloat(hash.speedFloat, 5.5f, speedDampTime, Time.deltaTime);
}
else
{
anim.SetFloat (hash.speedFloat, 0f);
}
}
void Rotating(float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);
rigidbody.$$anonymous$$oveRotation(newRotation);
}
void Audio$$anonymous$$anagement(bool shout)
{
if(anim.GetCurrentAnimatorStateInfo(0).nameHash == hash.locomotionState)
{
if(!audio.isPlaying)
{
audio.Play ();
}
}
else
{
audio.Stop();
}
if(shout)
{
AudioSource.PlayClipAtPoint(shoutingClip, transform.position);
}
}
}
Your code looks sound. In fact aside from some very $$anonymous$$or formatting differences it is exactly what is in the finished scripts for the S$$anonymous$$lth project. Out of curiosity, are the key in your Input $$anonymous$$anager for the Horizontal and Vertical axis reversed?
Answer by DanTup · Jan 01, 2014 at 05:15 PM
Have you just got your camera positioned on the wrong side of the character, so they directions are inverted?
Sometimes, on occasion the camera screws up like that. But the two are unrelated in my testing, it always has the wrong direction from the start.
THAN$$anonymous$$ YOU SO $$anonymous$$UCH FOR THIS CO$$anonymous$$$$anonymous$$ENT!!!
Answer by ben_friedman · May 30, 2014 at 07:26 PM
If you click on the "game" screen during testing, then your keys should work correctly. If you click on the "scene" screen to readjust the camera while you are testing the game, your keys will get reversed. Can anyone confirm that this is the symptom we are all having? If so, the quick solution is to click on the "game" screen while you're testing.
Answer by DyrdaOrg · Oct 08, 2013 at 07:47 AM
Go here:
http://unity3d.com/learn/tutorials/projects/stealth/player-movement
And try to copy-paste entire code... If it won't help - then problem is with Unity. If that helps - then the code has fault (although I have almost the same).
You can also go to:
YourProjectDirectory/Done/DoneScenes and fire up DoneStealth.unity
If problem occurs even then... well... maybe some kind of beverage was spilled on your keyboard or cat was sitting there? ;)
Yeah, the code isnt the problem. Also, when I fired up the done scene, i had the same problem. The character is facing correctly, just moving in the opposite direction. During sneak, it moves in the correct direction.
So, just to be sure, not only your scene
YourProjectDirectory/Scenes/S$$anonymous$$lth.unity
but also
YourProjectDirectory/Done/DoneScenes/DoneS$$anonymous$$lth.unity
is working incorrectly?
Yes. Both are working incorrectly. They also have the same problem, from what I can tell.
I'm sorry, but that means that the problem is with your/drivers, keyboard, laptop, etc.
Try to connect different keyboards - maybe this help cause that project was done by me in July and works, and recently was downloaded by my colagues and works perfectly in movement matter.
Does this mean the problem is isolated on my computer, and the project itself will work on other computers?
Answer by DannyBurbol · Oct 21, 2013 at 05:38 AM
I'm also walking through the Stealth tutorials and I'm getting exactly the same issue cooledcannon started this post with.
The interesting thing is, pressing the stealth key makes the character walk forward correctly and releasing it has the character moving in reverse again. (just as cooledcannon described)
this sounds like it's animation specific or state specific, however, when I go to the Animator tab and click on the transitions from Idle to Locomotion or from Idle to Sneak, the Preview clearly shows the character walking forward and stepping away from the origin of the Preview window.
small update with progress: I went into the Animator for the player and double-clicked the Locomotion node on the Base Layer.
This brings me to the Blend Tree with Walk and Run based on speed.
If I change the animations in the Blend Tree from Walk and Run, to Walk and Walk, the character walks in the correct direction (and never plays the run anim)
If I change the animations in the Blend Tree to Run and Run, the character runs in the reverse direction that they are facing (which is the issue what started this thread).
It seems to me that the Run anim has an issue. However I'm not sure how to identify or correct it.
I tried to compare humanoid_runForward_faceForward with humanoid_sneak and humanoid_walkForward_faceForward, but didn't see any obvious difference.
I also tried to experiment with check/uncheck of some of the params in the humanoid_runForward_faceForward inspector window, but it didn't seem to make any difference.
for now, I'm continuing the tutorial with just the walk anim.
Hey man, thanks for the help, its appreciated.
I decided to change the Run animation to another Walk animation. Its a bit slow, but things are working great. Do you know how to increase the walking speed?
Also, the problem is really weird. Sometimes I would always go backwards(maybe a very short tap makes it go forwards), sometimes always forwards, sometimes mix of forwards and backwards(which was incredibly annoying)
Okay, I thought I found a fix.... but it turns out it only works when pressing the play button in Unity. when I built and ran the final game... the character runs facing backwards.
for the record, here's what I tried....
in Hierarchy Tab: click on char_ethan.
in Inspector Tab: double-click PlayerAnimator
in the Animator Tab: in the "Base Layer", double-click Locomotion node.
in the Animator Tab: in the "Base Layer > Locomotion", click the other Locomotion node (the blend tree node)
in the inspector: originally this had "Walk" and "Run" entries (and in my previous post, I made a note about trying "Walk" and "Walk" vs "Run" and "Run")
restore this to have the entries "Walk" and "Run"
note, there are 4 columns in that little table: "$$anonymous$$otion", "Threshold", a clock, and a little man split in half.
I hovered over the clock icon and it said: "Changes animation speed"
I changed this column to read: 1 for Walk, and -1 for Run.
I repeat, this worked when testing in Unity... but once I did a Build and Run, the character was facing backward. :(
Wow... so that last failed experiment led me full circle.
I restored the Walk and Run to both use 1 and 1 for "Changes animation speed" column.
in Unity, when I use the play button, the character runs backwards in the preview window.
but if I do a "File" > "Build & Run", the character works correctly and runs forward.
So the issue is in the preview window... but not in the final game.
I'm using Unity 4, version 4.2.2f1
Your answer
Follow this Question
Related Questions
Slow vehicle to stop 1 Answer
How to disable A, S, and D keys for third person player movement 1 Answer
My player goes straight through my colider 3 Answers
My character moves when i dont want to 0 Answers
Movement based on camera 1 Answer