The question is answered, OP unresponsive.
How do I make a character walk backwards, rather than spinning to face the camera?
This is my first project in Unity. I am using the script as below (section of), with a simple Mecanim animator controller. It works fine - but I want to modify the character behaviour. As it is, when I press the up arrow, the character walks forward along the Z-axis away from the screen, and when I press the down arrow the character flicks around (no smooth movement) and walks toward the screen.
I'd like to ask the following questions:
--- Why does the character flip around? How is the target direction calculated?
--- I have a good backward walk animation. How would I modify the script so that on pressing the down arrow the character remains facing forward but executes the walk back animation?
Any help much appreciated.
------------ script fragment----------------
void Update () {
// Getting Input
_hDirection = Input.GetAxis("Horizontal");
_vDirection = Input.GetAxis("Vertical");
#region Camera Transform
//Get Main Camera Transfrom
Transform cameraTransform = Camera.main.transform;
//Get forward direction of the character
Vector3 _forward = cameraTransform.TransformDirection(Vector3.forward);
_forward.y = 0f; //Make sure that vertical direction equal zero
// Right vector relative to the character
// Always orthogonal to the forward direction vector
Vector3 _right = new Vector3(_forward.z, 0f, -_forward.x); // -90 degree to the left from
the forward direction
//If we are moving backward
if (_vDirection < 0) { _isMoveBack = true; }
else { _isMoveBack = false; }
#endregion
#region Calculating target movement
//Get target direction
Vector3 _targetDirection = (_hDirection * _right) + (_vDirection * _forward);
//If the target direction is not zero - mean there is no button pressing
if (_targetDirection != Vector3.zero) {
//Rotate toward the target direction
_moveDirection = Vector3.Slerp(_moveDirection, _targetDirection, rotationSpeed *
Time.deltaTime);
_moveDirection = _moveDirection.normalized; //Get only direction by normalize our
target vector
} else {
_moveDirection = Vector3.zero;
}
----------------------------End of fragment-----------------------
Thank you.
Also... Are there any good tutorials or books which include Unity character movement? I'd like to understand more of the vector maths which is used but it never seems to be explained in any depth. "Drop in this code" is the usual instruction - but if I don't understand what's going on, how can I modify it or correct it if something goes wrong? Thank you.
Crap... there's no answer... I wanted the same thing. I'll try to figure it out and if I do I'll let you know.
I realize this is a few months later, but since there is no answer I will try to help. First things first is this the whole script? Where does this variable comes from _is$$anonymous$$oveBack?
Answer by paranoidray · Nov 23, 2015 at 10:14 AM
The solution to this is harder than one would think.
Basically if you use the ThirdPersonController.prefab ( with the Ethan model ) it uses the animation motion to move the character.
This is the so called "Apply root motion" feature.
So if you want to continue to use the animation motion like the prefab does you need to find an animation that really walks backwards. There is no way to cheat or play the animation backwards as far as I could find after trying this for many hours.
Luckily Ethan contains a clip where he walks backwards, it is part of the walking clip.
The new Unity 5 animation system uses begin and end times on animation clips so you can use a selection on an otherwise longer animation clip that has multiple different motions.
So we need to create a clip selection with an begin and end time that only uses Ethan walking backwards.
Unfortunately I found no way to reuse the existing FBX with the clip, I had to manually duplicate the FBX file, ( You can do that in the file system or in Unity by pressing Ctrl-D )
Once you have duplicated the walking animation FBX file you can dive into the Grounded Animation state in the Animator window and add a new motion using the "Add Motion Field". Use the duplicated animation clip and click on Edit.
There you can move the begin and end sliders to select the short backwards animation. It is far to the end, I am using start 341 and end 371.
Finally select your blend tree and input the value -1 for Pos Y of your new motion.
That is it.
Now when you issue: m_Animator.SetFloat("Forward", -1, 0.1f, Time.deltaTime); ( Notice the -1 )
He will use the walk backwards animation and also move backwards.
I was looking for this too. Thanks for your help.
It worked perfectly!
Regards
And if you or anyone else would like to have a non grey Ethan, here is a nice texture:
http://mypandur.com/Tuts/Unity/30$$anonymous$$/EthanAlbedoSpecular.png
Update: It might be possible to use multiple selections on the same animation without copying the FBX file, see here for a possible solution:
https://youtu.be/4BUVe_1Cpac?t=557
I have not tested it myself however..
This would be extremely useful for my project; but I can't figure out where Ethan's animations are (the file we would need to duplicate - it doesn't seem to be the FBX file under ThirdPersonCharacter>$$anonymous$$odels ). Could you point me in the right direction?
@paranoidray This worked great; except for some reason it doesn't move backwards on floor quads that are rotated in specific angles in the Y axis, even though that obviously shouldn't have any effect; and even more strangely, it makes a difference whether the Y rotation is typed into the inspector as 0 or 360 even though the latter is converted immediately into 0. Any idea what would cause something like this? I don't think it's something in my own code because the movement is just handled by the stock blendtree (with one new entry added as you explained above, but that isn't my own code).
Here is the funning thing. I had same issue then I noticed that the speed on the animation was positive. I let the animator calculate the speeds. Simple fix is I manual set them all to negative.