Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Bowbowis · Jul 15, 2017 at 01:03 AM · rotationmovementturningslopes

Problem turning character on a sloped surface.

I'm working on a custom (non-rigidbody) character controller that is capable of rotating to move across any surface, including walls and ceilings, and interacting with terrain that uses a concave mesh collider. Unfortunately, I'm having some trouble getting the character to turn properly while standing on a slope.

In order to work properly the controller must be rotated twice in each step. The first rotation occurs when the player moves, it must gradually turn the character around his local Y-axis until he is facing the direction of the input relative to the camera direction. The character is then moved by speed * transform.forward and resolves any collisions that may occur. The second rotation coincides with ground detection, the character should be rotated so that his transform.up equals the normal of the surface he's currently standing on.

This is the closest I've come to code that does what I want but it has an unfortunate tendency to flip the character upside down:

 //Turn
      if (moveInput != vector3,zero)
     {
         Vector3 targetDirection = (moveInput.z * cam.transform.forward) + (moveInput.x * cam.transform.right);
         targetDirection.y = 0;
         targetDirection.Normalize();
 
         turnDirection = Vector3.Slerp(turnDirection, targetDirection, Time.fixedDeltaTime * player.trn);
         transform.rotation = Quaternion.FromToRotation(Vector3.forward, turnDirection) * Quaternion.FromToRotation(Vector3.up, transform.up);
 
     //Move by speed * transform.forward.
     }
 
      //Resolve collisions
 
      //Ground detection
         Vector3 origin = transform.position + (20 * transform.up);
 
         if (Physics.Linecast (origin, origin - (transform.up * 36), out contact) && Vector3.Angle (contact.normal, transform.up) <= 45)
         {
             transform.position = contact.point;
             ground = true;
             transform.rotation = Quaternion.FromToRotation(Vector3.forward, turnDirection) * Quaternion.FromToRotation(Vector3.up, contact.normal);
             Debug.DrawLine(origin, origin - (36 * transform.up), Color.green);
         }
     }

The flipping seems to occur most frequently when the character is on certain parts of the collider, or when there is no collision with the ground at all, but I haven't been able to replicate the issue consistently.

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
0
Best Answer

Answer by Bowbowis · Jul 15, 2017 at 09:44 PM

I think I figured it out. The solution seems to have been changing:

 transform.rotation = Quaternion.FromToRotation(Vector3.forward, turnDirection) * Quaternion.FromToRotation(Vector3.up, transform.up);

and

 transform.rotation = Quaternion.FromToRotation(Vector3.forward, turnDirection) * Quaternion.FromToRotation(Vector3.up, contact.normal);

to

 transform.rotation = transform.rotation * Quaternion.FromToRotation (prevDirection, turnDirection);

and

 transform.rotation = Quaternion.FromToRotation (transform.up, contact.normal) * transform.rotation;

respectively.

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

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

How to move as well as rotate the object to face the direction it is moving. 0 Answers

Multiplayer Rotation is really slow 1 Answer

How do I make a vehicle turn properly? 1 Answer

Mathf.Lerp working in one direction but not the other 1 Answer

How to make a motorbike turning mechanism? Here is my current movement script that has a very bad turning mechanism. 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