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 /
avatar image
0
Question by Saiura · Jun 05, 2014 at 10:02 AM · animationmovementdirectiondirectional animating

Problem with character facing towards movement direction

Hello everyone, I'm pretty new to Unity, so I have quite some trouble with various parts, this being one of them.

I am currently making a 2,5D Sidescroller, I have a following camera set up, an own playermodel with proper animations. Following various tutorials (the one I started with was never finished), I am now trying to have my character face the direction he is moving in. A common problem from what I have seen, but even with the other similiar topics I didn't manage to get it fixed on my end.

I currently use the following code to control the animations and the direction my character should face:

 public class AnimationChooser : MonoBehaviour {
     
     Animator anim; 
 
     void Awake () 
     {
         anim = GetComponent<Animator> ();
     }
     
 
     void Update () 
     {
         float running = Input.GetAxis ("Vertical");
         float jumping = Input.GetAxis ("Jump");
         float dashing = Input.GetAxis ("Dash");
 
         float dir = Input.GetAxisRaw ("Vertical");
 
         if (dir != 0) 
         {
             if (dir > 0)
             {
                 transform.eulerAngles = Vector3.up * 90;
             }
             else
             {
                 transform.eulerAngles = Vector3.up * 270;
             }
         }
 
         anim.SetFloat ("Speed", running*running);
         anim.SetFloat ("Jumping", jumping*jumping);
         anim.SetFloat ("Dashing", dashing*dashing);
     }
 }

Oddly enough, the character nicely faces the proper direction for each movement direction (left and right), but he only moves to the right, no matter what I press.

Also, the reason for using the vertical axis and multiplying with 90 and 270 is that I had to turn my model by 90 degrees in the first place. Without that, he moved into and away from the camera instead of left and right.

Any help on this is highly appreciated, so thanks in advance.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Saiura · Jun 07, 2014 at 08:48 AM

This is less than an answer, more a little push (sorry about that, but I really need to get this solved)

I currently have it like this with the turning:

     public float smooth = 15.0F;
     void LateUpdate () {
         // Rotation
         
         Quaternion targetf = Quaternion.Euler(0, 270, 0); // Vector3 Direction when facing frontway
         Quaternion targetb = Quaternion.Euler(0, 90, 0); // Vector3 Direction when facing opposite way
         
         if (Input.GetAxisRaw ("Vertical") < 0.0f) // if input is lower than 0 turn to targetf
         {
             transform.rotation = Quaternion.Lerp(transform.rotation, targetf, Time.deltaTime * smooth);    
         }
         if (Input.GetAxisRaw ("Vertical") > 0.0f) // if input is higher than 0 turn to targetb
         {
             transform.rotation = Quaternion.Lerp(transform.rotation, targetb, Time.deltaTime * smooth);
         }
     }

just changed the Slerp for a Lerp to get a waaaay faster rotation (no idea why, but it works).

My old problem still persists though... the character does turn into the direction of the input key, but only moves in one direction... If I press the right arrow key, the character faces and moves towards the right... If I press the left arrow key, the character faces to the left but "moon-walks" to the right.

How can I fix that?

Would it be possible to just create inverse movement for the left and right arrow key for a certain facing-direction?

Edit:

I solved it now, went to the FPSInputController

There I put:

         if (Input.GetKey("d") || Input.GetKey("right")) 
             {
                 directionVector = directionVector * directionLength;
             }
             if (Input.GetKey("a") || Input.GetKey("left")) 
             {
                 directionVector = -directionVector * directionLength;
             }
             else
             {
                 directionVector = directionVector * directionLength;
             }

right where there was just

 directionVector = directionVector * directionLength;

before.

Comment
Add comment · 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
0

Answer by Stygtand · Jun 05, 2014 at 11:20 AM

Im not sure which way is which from the looks of your script. But this is the solution i use. Attach this to a child gameobject of the object that has the input controller. That way, your character controller (which controls the inputs) is moving back and forth, yet your child (this rotation game object) hold its relative transform position, but you change the rotation for the object and its child (which holds the model). This script also turn the object slowly. Change smooth float to a lower float for faster turn speed.

 public float smooth = 2.0F;
 void LateUpdate () {
         // Rotation
         
         Quaternion targetf = Quaternion.Euler(0, 0, 0); // Vector3 Direction when facing frontway
         Quaternion targetb = Quaternion.Euler(0, 180, 0); // Vector3 Direction when facing opposite way
         
         if (Input.GetAxisRaw ("Vertical") < 0.0f) // if input is lower than 0 turn to targetf
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, targetf, Time.deltaTime * smooth);
             
         }
         if (Input.GetAxisRaw ("Vertical") > 0.0f) // if input is higher than 0 turn to targetb
         {
             transform.rotation = Quaternion.Slerp(transform.rotation, targetb, Time.deltaTime * smooth);
             
         }
     }
Comment
Add comment · Show 6 · 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 Saiura · Jun 05, 2014 at 01:00 PM 0
Share

Well, I created an empty, put the script onto the empty and then dragged it on my player (in the hierarchy), I am pretty sure I did miss something or did something wrong cause... well... it doesn't do a thing

avatar image Stygtand · Jun 06, 2014 at 08:32 AM 0
Share

First you have your game object with your input. then create a empty game object, you can call it rotation if you wish. Put this script onto it. Then place your mesh render / your model in a child of this.

 [Character] : insert your controllers here
   [Rotation] : insert rotation
     [$$anonymous$$odel] : insert model

This way, when your char object moves its transformation. All the child objects of the character will follow the transform. [Rotation] scripts then rotate itself and childs below. Lastly the model shows the [model], which we move from character and rotate from [rotation].

avatar image Saiura · Jun 06, 2014 at 08:53 AM 0
Share

well, my character model holds all the character scripts. the Input, $$anonymous$$otor, Character-controller and some self made scripts are all in the very same model at the moment, I will see to change it up a bit.

I also just tried to put your script into the same spot I had my previous rotation script (with some slight number changes). funny enough, the character now rotates when I move in the other direction. Though when I turn around, the character turns very slow, even at smooth on 0,1f. Also another problem is that while turning, he moves into the direction he faces during the turn, causing him to fall off the platform.

The problem with the movement direction flipping consists like that still though

avatar image Saiura · Jun 06, 2014 at 09:10 AM 0
Share

I also tried adding a new object, giving it the input, motor, character controller scripts, making my empty for the rotation a child of it and placing my character model als child of the rotation-empty. Though now, ins$$anonymous$$d of moving probably, my character floats above the ground and as soon as I move, he flies faaar away in a slight curve to the side.

As another option... is there a way to say something like, swap the direction the character moves at a specific degree of turning? like... if character is turned by X degrees, inputkey Y moves in - direction on the vertical axis?




  • I added another comment below about the results

avatar image Stygtand · Jun 06, 2014 at 11:53 AM 0
Share

I think you got it at the second last comment. but you need to speed up the rotation speed, and force your z axis to 0 while turning. try set smooth to like 10 or 15f,

do a "transform.poistion.z = 0f" this will force position z to be 0 always.

the reason for rotation script to be on a child of the controls is, that even though the character turns, the controls stays the same. I hope it helps.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Third-person character animations based on World-relative input and Y-rotation (looking at mouse position) 1 Answer

Character facing wrong direction when moving vertically 0 Answers

Getting animations to play relative to player's direction? 4 Answers

I have a value that determines which way my character is facing in degrees. How do I convert that value to a range of -1 to 1 for smooth mecanim bleend tree animation? 0 Answers

How To Lock Direction While Jumping? 1 Answer


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