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 Kyragos · Oct 13, 2013 at 03:13 PM · rotationcharactercontroller

Tilt character with the ground and rotate it

Hi.

I am making a game where the character tilts with the ground. I managed to do it pretty smoothly with the code below which uses Raycasts from 4 sub-objects. For some reasons, it is best I keep a Character Controller.

 public Transform backLeft;
 public Transform backRight;
 public Transform frontLeft;
 public Transform frontRight;
 private RaycastHit lr;
 private RaycastHit rr;
 private RaycastHit lf;
 private RaycastHit rf;
  
 private Vector3 upDir;
  
 void Update () {
  
     Physics.Raycast(backLeft.position + Vector3.up, Vector3.down, out lr);
     Physics.Raycast(backRight.position + Vector3.up, Vector3.down, out rr);
     Physics.Raycast(frontLeft.position + Vector3.up, Vector3.down, out lf);
     Physics.Raycast(frontRight.position + Vector3.up, Vector3.down, out rf);
  
     upDir = (Vector3.Cross(rr.point - Vector3.up, lr.point - Vector3.up) +
       Vector3.Cross(lr.point - Vector3.up, lf.point - Vector3.up) +
       Vector3.Cross(lf.point - Vector3.up, rf.point - Vector3.up) +
       Vector3.Cross(rf.point - Vector3.up, rr.point - Vector3.up)
       ).normalized;
  
     transform.up = upDir;
  
 }

But the problem now is that the character can't rotate anymore (I am using transform.Rotate with left/right arrow keys). I understand this is because I reassign transform.up, but I haven't found a solution for now. I've tried several things, but none worked. The most satisfying was using a temporary variable to keep the rotation on y axis, but then the character isn't aligned with the correct normal anymore after he changes his starting rotation.

Has someone got an idea?

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

Answer by whydoidoit · Oct 13, 2013 at 04:13 PM

Rotate the position of the up vector like this:

    transform.rotation =  Quaternion.FromToRotation(transform.up, upDir) * transform.rotation;
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 robertbu · Oct 13, 2013 at 04:32 PM 0
Share

I believe the order matters in this one:

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

I've always assumed (based on a few tests) that assigning to transform.up was doing a FromTo under the hood. Not the case?

avatar image whydoidoit · Oct 13, 2013 at 07:04 PM 0
Share

Really - that's not what I do - so given that the transform.up is already transformed, surely you should apply the rotation of it to that?

avatar image robertbu · Oct 14, 2013 at 01:47 AM 1
Share

You have a much deeper understanding of Unity than I do, so I keep thinking I'm missing something. But I cannot see this order working. Apply the following script to any object where you can identify the top of the object. It generates a random rotation then attempts to align the resulting object with Vector3.up. Left mouse button has FromToRotation() first, right mouse button has it second.

 #pragma strict
 
 function Update () {
 
     if (Input.Get$$anonymous$$ouseButtonDown(0)) {
         Debug.Log("FromTo exectued first");
         transform.rotation = Random.rotation;
         transform.rotation = Quaternion.FromToRotation(transform.up, Vector3.up) * transform.rotation;
     }
     else if (Input.Get$$anonymous$$ouseButtonDown(1)) {
         Debug.Log("FromTo exectued second");
         transform.rotation = Random.rotation;
         transform.rotation = transform.rotation * Quaternion.FromToRotation(transform.up, Vector3.up);
     }
 }
avatar image whydoidoit · Oct 14, 2013 at 02:10 AM 0
Share

I must be double negging my self (do it the opposite way to the way you feel it should work! Guess I've got used to it feeling the right way).

I just did this today and did it the other way around which mostly works, but of course you are right, everywhere else I have it is the correct way around. Ugh, that's what comes of not sleeping enough and being stuck at the keyboard.

avatar image Kyragos · Oct 14, 2013 at 03:36 PM 0
Share

Indeed, the solution of robertbu seems to be working perfectly. Thank you very much.

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

17 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

Related Questions

Camera lock on enemy 0 Answers

CharacterController "breaks" collision, goes haywire 2 Answers

Face Direction Movement using CharacterController 1 Answer

Rotate a vector3 to a surface normal? 1 Answer

rotate around character 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