Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 /
  • Help Room /
avatar image
0
Question by ngerbens · Jun 29, 2021 at 01:00 PM · rotationaxiscubeeuleranglesforward

How to calculate and use new direction angle as a new forward direction.

My player is a cube tumbling forward/backward and sideways.

This script calculates the edges of my cube and make it rotate around its edge.

 void FixedUpdate()
         {
             bound = GetComponent<BoxCollider>().bounds;
 
             left = new Vector3(-bound.size.x / 2, -bound.size.y / 2, 0);
             right = new Vector3(bound.size.x / 2, -bound.size.y / 2, 0);
             up = new Vector3(0, -bound.size.y / 2, bound.size.z / 2);
             down = new Vector3(0, -bound.size.y / 2, -bound.size.z / 2);

//These are the 'positionToRotations' when pressed by arrows) }



         IEnumerator Roll(Vector3 positionToRotation)
         {
             isRolling = true;
 
             float angle = 0.0f;
             Vector3 point = transform.position + positionToRotation;
             Vector3 axis = Vector3.Cross(Vector3.up, positionToRotation);
 
             while (angle < 90f)
             {
                 float angleSpeed = Time.deltaTime + rotationSpeed;
                 transform.RotateAround(point, axis, angleSpeed);
                 angle += angleSpeed;
                 yield return null;
             }
             transform.RotateAround(point, axis, 90 - angle); 
             isRolling = false;
         }

(What I like about this function is that I can change the size of the cube and make it a rectangle too)

But when my cube is randomly rotated (Y axis) in the world space, it tries the find the wrong 'point' and 'axis'. So I need to calculate where these are and (even more complicated for me) write a line how to use them. This picture illustrates what I'm stuck at.



alt text



I have no idea how this works in the mathematical way. I tried a lot of Quaternion.AngleAxis stuff, but I don't fully understand the math behind it.

schermafbeelding-2021-06-29-om-144008.png (144.6 kB)
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

Answer by ngerbens · Jul 02, 2021 at 11:16 AM

Well, since I figured out how this works, I'd just answer it myself for everyone who might encounter this post in the future....

This is how it works

      IEnumerator Roll(Vector3 positionToRotation)
         {
             isRolling = true;
     
             //GroundLevel
             RaycastHit hit;
             Physics.Raycast(thisRigidBody.position, Vector3.down, out hit);
     
             //RB gravity
             thisRigidBody.useGravity = false;
     
             Vector3 cam = Camera.main.transform.forward; cam.y = 0;
     
             float angle = 0.0f;
             //This calculates the perpendicular of the cam forward
             float angleNeeded = Mathf.Atan2(Vector3.Dot(Vector3.up, Vector3.Cross(newUp, cam)), Vector3.Dot(newUp, cam)) * Mathf.Rad2Deg;
             
     Vector3 point = transform.position + positionToRotation; 
             Vector3 groundPoint = new Vector3(point.x, hit.point.y, point.z);
     
             Vector3 axis = Vector3.Cross(Vector3.up, positionToRotation);
             Vector3 camAxis = Quaternion.AngleAxis(angleNeeded, Vector3.up) * axis;
     
             while (angle < 90f)
             {
                 float angleSpeed = Time.deltaTime + rotationSpeed;
                 transform.RotateAround(groundPoint, camAxis, angleSpeed);
                 angle += angleSpeed;
                 yield return null;
             }
             transform.RotateAround(groundPoint, camAxis, 90 - angle);
     
             thisRigidBody.useGravity = true;
             isRolling = false;
         }

Comment
Add comment · Show 2 · 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 ngerbens · Jul 02, 2021 at 11:21 AM 0
Share

'newUp' is equivalent to 'up' in the FixedUpdate. But I turn up into newUp depending on the rotation of the camera in another section.

avatar image ngerbens · Jul 05, 2021 at 06:38 PM 0
Share

This Answer is only one part of the answer: I still need to figure out how the 'point' is replaced in the right position.

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

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

Quaternions: Setting rotation of one gameObject to Y,Z-Axis rotation of gameObject-A and Z-Axis position of gameObject-B 0 Answers

Using Local Angles to target enemy object 0 Answers

I need help in rotating cube by 90 degree with swipe in all axis 0 Answers

Have angle problem in rigidbody.moveRotation 0 Answers

Quaternions: Setting rotation of one gameObject to Y,Z-Axis rotation of gameObject-A and Z-Axis position of gameObject-B 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