Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Sebane1 · Oct 16, 2015 at 11:38 AM · rotationquaternionaxislocalrotationlocaleulerangles

Unity Simulate Local Rotation

I've run into a situation where I need to be able to use a relative Y rotation for my player. This is so the player can walk on walls, floors, and ceilings and operate "the same way" regardless of actual rotation (I've already got gravity and such sorted out). The game in which I'll be using this is an FPS if that info is relevant.

I came across this: http://answers.unity3d.com/questions/518399/simulate-child-parent-relationship.html

But it doesn't quite apply to what I'm trying to accomplish.

This is my present code for turning the player, but it isn't suited for different orientations (it successfully works between a floor or ceiling for obvious reasons though).

  angle = new Vector3(_yMouseAxisLocked ? transform.localEulerAngles.x : currentYPos += yCoord,
  xMouseAxisLocked ? transform.localEulerAngles.y : currentXPos += xCoord,
 transform.localEulerAngles.z);
 transform.localRotation = Quaternion.Euler(angle);

Ideally, I'd be able to pass the resulting Vector3 in the above code into a method that does the work of figuring out what value each axis should be set to based on the players current orientation.

Something like:

     Quaternion RelativeRotation (Quaternion baseRotation, Quaternion relativeRotation){
        // Do math
        // return final result
     }

My usage of the method would look something like

  angle = new Vector3(_yMouseAxisLocked ? transform.localEulerAngles.x : currentYPos += yCoord,
  xMouseAxisLocked ? transform.localEulerAngles.y : currentXPos += xCoord,
 transform.localEulerAngles.z);
 transform.localRotation = RelativeRotation(Quaternion.Euler(_rotation), Quaternion.Euler(angle));

Its been quite a puzzle trying to find anybody in the same boat as me. I'll admit I'm not math savvy or that knowledgable when it comes to Quaternions.

This image should clarify the application of this.

alt text

Note: Take no mind to my present code using local rotation. The script this is from runs on game objects with parents, and game objects without parents. A check will be added to determine if the game object has a parent or not.

Edit:

Started toying with the following code. Only partially works when on walls, If I look at a veiwing angle greater than 45 degrees either direction the player starts spinning out of control.

     if (isOutsideGameObject) {
     angle = new Vector3(_yMouseAxisLocked ? transform.localEulerAngles.x : currentYPos += yCoord,
                          _xMouseAxisLocked ? transform.localEulerAngles.y : currentXPos -= xCoord,
                          transform.localEulerAngles.z);
                          Quaternion startParentRotationQ = Quaternion.Euler(new Vector3(
     !_yMouseAxisLocked ? angle.x : 0, !_xMouseAxisLocked ? angle.y : 0, 0));
                              transform.rotation = (Quaternion.Euler(player._rotation) 
 * Quaternion.Inverse(startParentRotationQ)) 
     * startChildRotationQ;
             } else {
             angle = new Vector3(_yMouseAxisLocked ? transform.localEulerAngles.x : currentYPos += yCoord,
                                _xMouseAxisLocked ? transform.localEulerAngles.y : currentXPos += xCoord,
                                transform.localEulerAngles.z);
                                 transform.localRotation = Quaternion.Euler(angle);
             }









dc9d5d0e85f4f4b299023b86a8ef05a9-2.jpg (172.3 kB)
Comment
Add comment · Show 1
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 Owen-Reynolds · Oct 16, 2015 at 05:02 PM 0
Share

Best general advice might be to learn about using a Local Frame of Reference (just general math); and how, in Unity, to use Quaternion.FromToRotation, Quat.Inverse and transform.transformPoint and inverseTransformPoint, to go between global and local frames of reference.

I'm also a little puzzled by the use of += inside those ?:'s. That's one of those things which could be incredibly clever, but is really almost always not what you wanted.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Complicated Rotation Issue 1 Answer

Clamped Turret Doesn't Want to Lerp the Other Way 2 Answers

Finding the Euler angles of an object's rotation in terms of a different set of axes 0 Answers

How can I pitch and roll a circular platform without releasing vertical/horizontal input? 1 Answer

Code to rotate around a local axis until local Y is 0? 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