Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Nov 30, 2015 at 03:16 PM by Le-Pampelmuse for the following reason:

The question is answered, OP unresponsive.

avatar image
0
Question by Sulla__2014 · Apr 23, 2015 at 04:12 PM · character movementbackwards

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sulla__2014 · Apr 22, 2015 at 08:38 PM 0
Share

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.

avatar image NIOS · Aug 18, 2015 at 02:00 AM 0
Share

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.

avatar image fredz0004 · Nov 23, 2015 at 07:21 AM 0
Share

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?

1 Reply

  • Sort: 
avatar image
7

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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xptohz3_turbo · Nov 30, 2015 at 11:17 AM 0
Share

I was looking for this too. Thanks for your help.

It worked perfectly!

Regards

avatar image paranoidray Xptohz3_turbo · Nov 30, 2015 at 11:58 AM 0
Share

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

avatar image paranoidray · Nov 30, 2015 at 12:00 PM 0
Share

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..

avatar image HonoraryBob · May 02, 2016 at 12:52 AM 0
Share

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?

avatar image HonoraryBob · Jul 19, 2017 at 09:22 PM 0
Share

@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).

avatar image Wraith-Fades · Aug 07, 2017 at 04:29 AM 0
Share

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.

Show more comments

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

First person controller going backwards 0 Answers

How to start an animation in script? 2 Answers

,Scenes is displaiyed with gap 1 Answer

Terrain Problem! Character begins to wobble extremely! 2 Answers

Problem with character controller movement and gravity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges