• 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 /
avatar image
0
Question by AlexT_IH · Aug 14, 2016 at 02:18 PM · scripting problemmovementflippingunity 5.1.1

WSAD Movement performs unwanted flipping of character

I am making a 3rd person shooter game, and the following script part is supposed to handle the character's movement and animation (I have a rather old type of model available for my game's purposes), using the Arrow Buttons or the WSAD keys. It is supposed to simply apply rotation (left/right) to the character while moving it forward.

 void AnimMovement()
     {
         //We receive the raw values for the Horizontal and Vertical input axes (-1, 0 or 1)...
         var movementV = GetInputValues();
         float h = movementV.first;
         float v = movementV.second;
 
         //And the current state of the player's animator.
         var curState = playerAnimator.GetCurrentAnimatorStateInfo(0);
         
 
         //For optimization purposes, if there is no input (h = v = 0), then we return here.
         if (h == 0f && v == 0f)
         {
             //We are not moving.
             playerAnimator.SetFloat("Speed", 0f);
             return;
         }
 
         
         //if (h != PrevH)
         playerAnimator.SetFloat("Speed", 1f);
 
 
         //We will move our player, only if the current state of their Animator is the LocomotionState.
         //This is to prevent our player from moving when dead.
         if (curState.shortNameHash == Hashes.LocomotionHash)
         {
             cc.SimpleMove(transform.forward * speed);
         }
 
         if(tag != Tags.AIPlayerController)
             ApplyRotation(h, v);
 
         PrevH = h;
         PrevV = v;
     }
 
     void ApplyRotation(float h, float v)
     {
         //We need to be able to turn around the Y axis using the arrow/WASD keys,
         //so we'll make a rotation that enables this.
         float turnVal = (h == 0f) ? v : h;
         tiltZ = Utilities.ClampAngle(tiltZ, 0f, 360f) + turnVal;
         Vector3 lv = new Vector3(h, 0, v);
         var nrot = Quaternion.LookRotation(lv,transform.up);
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(transform.rotation.eulerAngles + nrot.eulerAngles), Time.deltaTime * turningSpeed);
     }
 
     Pair<float, float> GetInputValues()
     {
         float h, v;
         if (gameObject.tag == Tags.PlayerController)
         {
             //PlayerController input.
             h = Input.GetAxisRaw("Horizontal");
             v = Input.GetAxisRaw("Vertical");
         }
         else
         {
             //AIPlayerController input.
             h = nav.nextPosition.x;
             v = nav.nextPosition.z;
         }
         
         return new Pair<float, float>(h, v);
     }

The problem is that, while at first the character moves well, after a while it simply goes upside down. I suspect it has something to do with the rotation being applied to the character, but I am stumped.

Does anybody have any idea why this is happening?

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Simultaneously moving using two inputs W D or Up LEFT to move diagonally 0 Answers

Hit a wall in my RTS movement controller. Issues with MoveToward and Coroutine logic. 0 Answers

Unity Space Shooter Tutorial Player Will Not Move 1 Answer

Script problems c# 0 Answers

How to stop movement script on void start and resume after. 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