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 Xcalibur · Oct 09, 2011 at 10:28 PM · c#rotationcharactercontrollerquaternion

Character movement always faces forwards

Hi all,

After making a lot of progress today getting a character to move on a plane responding to clicks, I've hit a wall when it comes to rotation and direction. I now have the character moving nicely and a smooth rotation using Quaternion.Slerp, but it always 'turns back to the front' direction during the move.

My face function:

 // Make the character face the direction of movement
 void FaceMovementDirection( )
 {
     
     Quaternion targetRotation = Quaternion.LookRotation ( targetLocation - thisTransform.position );
     
     targetRotation.z = 0;
     targetRotation.x = 0;
     //targetRotation.y = 0;
     
     float smooth = 2.0F;
     
     Vector3 tempVector = targetLocation - thisTransform.position;
     
     if (tempVector.magnitude > 0.1)
     {
         thisTransform.rotation = Quaternion.Slerp (thisTransform.rotation, 
                                                    targetRotation,
                                                    smooth * Time.deltaTime);
     }
     
 }

The move function:

 // Control the character
 void CharacterControl()
 {
     
     // Check to see if a new destination has been found
     if (state == (int)ControlState.FoundNewDestination )
     {
         
         // get a new movement position
         RaycastHit hit;
         Ray ray = cam.ScreenPointToRay ( Input.mousePosition ); // Get the position
         if ( Physics.Raycast (ray, out hit) )
         {
             
             // Found a new position, now is it a valid target?
             float clickDist = ( transform.position - hit.point ).magnitude;
             
             if (clickDist > minimumDistanceToMove )
             {
                 
                 Debug.Log ("Distance to Target:" + clickDist);
                 targetLocation = hit.point;
                 moving = true;
                 
             }
             
         }
         
     }
     
     
     Vector3 moveDirection = Vector3.zero; // declares an empty movement vector
     
     if ( moving )
     {
         
         if (character.isGrounded) 
         {
             
             moveDirection = targetLocation - thisTransform.position;
             moveDirection.y = 0;
             
             moveDirection = thisTransform.TransformDirection(moveDirection);
             moveDirection *= speed;
             
         }
         
         moveDirection.y -= gravityForce * Time.deltaTime;
         character.Move ( moveDirection * Time.deltaTime );
         
         
     }
     
     FaceMovementDirection();
     
 }

Any and all help would really be appreciated!

Comment
Add comment · Show 5
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 syclamoth · Oct 09, 2011 at 10:35 PM 0
Share

If you make sure you only change 'targetRotation' when the inputs are more than a certain amount, it'll stop your character from 'resetting' whenever you aren't moving.

avatar image Xcalibur · Oct 09, 2011 at 11:25 PM 0
Share

Thanks for the input syclamoth! I did put in an 'if (tempVector.magnitude > 0.1)' call before setting the transform.rotation on the character... was that what you meant?

avatar image Xcalibur · Oct 09, 2011 at 11:27 PM 0
Share

Another note: I worked through it in my head, and it appears that the calculations are always ending with the same character rotation they start with - the targetRotation seems to be constantly 'forward' on the original alignment.

avatar image syclamoth · Oct 09, 2011 at 11:52 PM 0
Share

Or, you could store a Vector3 'last$$anonymous$$ovement' which is a normalized version of the last non-zero input, and use that.

avatar image Xcalibur · Oct 10, 2011 at 01:19 PM 0
Share

I stored a normalized last$$anonymous$$ovement Vector3, and I've tried about 6 different ways to get this working right with varying levels of ineffectiveness = )

I think the problem lies with my movement code, not the rotation, as the destination seems to always be 'front facing' no matter what I do.

Another strange thing is that whenever I do LookAt rotation calls (vector, quaternion, etc...) I get the model rotating and 'looking down' unless I zero out the X and Y coordinates of the last$$anonymous$$ovement or targetPosition vectors. I'm not entirely used to the system yet, but it seems strange.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Xcalibur · Oct 10, 2011 at 11:42 PM

My instinct above was right, the problem was with my movement code! I was modifying the moveDirection twice (once with the targetLocation - thisTransform.position, but AGAIN withthisTransform.TransformDirection(moveDirection) ). Once I removed the second moveDirection modification it all started to fall together.

I also added a magnitude check for the lastMovementDirection and a state to recognize when the character reached the destination. It now works 100%!

Thanks for the input syclamoth, much appreciated.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Resetting rotation with Quaternion.Euler 1 Answer

Flip over an object (smooth transition) 3 Answers

Rotation from AngleAxis() ToAngleAxis() flips back and forth 1 Answer

How to always rotate clockwise 1 Answer

Keep objects current rotation, with Input Mouse X 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