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
1
Question by OneShotGG · Jan 22, 2014 at 07:49 PM · mousemecanimroot motionturning

Mecanim "turnonspot" with mouse and not WASD

Ok so I am trying to get my character to execute the "turn on spot" animations found in the locomotion demo using the mouse and not the horizontal and vertical axis (wasd).

This is the code that works for WASD (as found in the locomotion demo DoJoy Class)

  Vector3 rootDirection = root.forward;
     
     float horizontal = Input.GetAxis("Horizontal");
     float vertical = Input.GetAxis("Vertical");
     
     Vector3 stickDirection = new Vector3(horizontal, 0, vertical);
     Vector3 CameraDirection = camera.forward;
     CameraDirection.y = 0.0f; // kill Y
     
     referentialShift = Quaternion.FromToRotation(Vector3.forward, CameraDirection);
     
     moveDirection =  referentialShift * stickDirection;
     Vector2 speedVec =  new Vector2(horizontal, vertical);
     speed = Mathf.Clamp(speedVec.magnitude, 0, 1);
     
     if (speed > 0.01f){ // dead zone
     Vector3 axis = Vector3.Cross(rootDirection, moveDirection);
     direction = Vector3.Angle(rootDirection, moveDirection) / 180.0f * (axis.y < 0 ? -1 : 1);
     } else {
     direction = 0.0f
     }

The above works as it should; the direction float goes from -1 to 1 based your change in direction and then settles back down to 0 if you are walking straight.

Now, I want to do the same thing but use the mouse instead of WASD.

So I attempted this:

 Vector3 rootDirection = root.forward;
 float x = Input.GetAxis ("Mouse X");
 float y = Input.GetAxis ("Mouse Y");
 Vector3 mouseDirection = new Vector3(x,0,y);
 
 Vector3 CameraDirection = camera.forward;
 CameraDirection.y = 0.0f; // kill Y
 referentialShift = Quaternion.FromToRotation(Vector3.forward, CameraDirection);
 
 aimDirection =  referentialShift * mouseDirection;
 
 Vector3 axis = Vector3.Cross(rootDirection, aimDirection);
 direction = Vector3.Angle(rootDirection, aimDirection) / 180.0f * (axis.y < 0 ? -1 : 1);

This does not work correctly as direction does not range from -1 to 1. Instead turning right ranges from .5 to 0 (backwards), turning left ranges from -.5 to .5 and "center" is .5 (should be 0.

So yeah, its all screwed up and I have tried changing a ton of stuff around to get it to function correctly but it seems like I am missing something.

For thoroughness here is the whole class as I have it right now.

//Root motion Controller

 public static void DoJoy(Transform root, Transform camera, ref float speed, ref float direction, ref bool aim, ref float idleDirection, ref Quaternion referentialShift, ref Vector3 moveDirection, ref Vector3 aimDirection)
     {
         SoldierCamera soldierCam = camera.GetComponent<SoldierCamera>();
         Vector3 rootDirection = root.forward;
         Vector3 torsoDirection = soldierCam.target.forward;
 
         float horizontal = Input.GetAxis("Horizontal");
         float vertical = Input.GetAxis("Vertical");
         Vector3 stickDirection = new Vector3(horizontal, 0, vertical);
         float x = Input.GetAxis ("Mouse X");
         float y = Input.GetAxis ("Mouse Y");
         Vector3 mouseDirection = new Vector3(x,0,y);
         // Get camera rotation.    
         Vector3 CameraDirection = camera.forward;
         CameraDirection.y = 0.0f; // kill Y
         referentialShift = Quaternion.FromToRotation(Vector3.forward, CameraDirection);
 
         // Convert joystick input in Worldspace coordinates
         moveDirection =  referentialShift * stickDirection;
         aimDirection =  referentialShift * mouseDirection;    
         Vector2 speedVec =  new Vector2(horizontal, vertical);
         speed = Mathf.Clamp(speedVec.magnitude, 0, 1);      
 
         if (speed > 0.01f){ // dead zone
             Vector3 axis = Vector3.Cross(rootDirection, moveDirection);
             direction = Vector3.Angle(rootDirection, moveDirection) / 180.0f * (axis.y < 0 ? -1 : 1);
         }else{
             Vector3 axis = Vector3.Cross(rootDirection, aimDirection);
             direction = Vector3.Angle(rootDirection, aimDirection) / 180.0f * (axis.y < 0 ? -1 : 1);
         }
 
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by OneShotGG · Jan 22, 2014 at 10:59 PM

I fixed the problem by only using this for DoJoy.

     Vector3 rootDirection = root.forward;
     Vector3 CameraDirection = camera.forward;
     CameraDirection.y = 0.0f; // kill Y
      
     Vector3 axis = Vector3.Cross(rootDirection, CameraDirection);
     direction = Vector3.Angle(rootDirection, CameraDirection) / 180.0f * (axis.y < 0 ? -1 : 1);
       
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

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

18 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

Related Questions

Mecanim Issues: Root motion not being applied, and weapon is out of place 2 Answers

Can I make character movement entirely with Mecanim's root motion? 0 Answers

Mecanim root motion disabled when coroutine kicks in 1 Answer

Root Motion issue 1 Answer

Mecanim - 4.3 deltaPosition Bug 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