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 fred_gds · Feb 07, 2017 at 06:30 AM · vector3directionanglerotatearound

from Quaternion to RotateAround

Hey,

I am trying to rotate my object so that it's Vector3.up points int the same direction as my Normal vector (so that the object stays parallel to the ground). So I have already calculated my normal and I use the following code to position my object:

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

The problem is that for other reasons the pivot of my transform is not in it's center, so the question is how can I rotate the object around it's physical center to match the normalVector.

I thought about using something like transform.RotateAround(), but I kind of don't know what to input as I would have to calculate some kind of angles and I do not know how to get those...

Also important I need to keep my current rotation around the Y-axis so that the object looks in the right direction.

Comment
Add comment · Show 6
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 hexagonius · Feb 07, 2017 at 08:01 AM 0
Share

when you use LookRotation, you don't have to rotate, you generate the final rotation. And you can use what you have. just use transform.forward as first and normala second parameter

avatar image fred_gds hexagonius · Feb 07, 2017 at 08:06 AM 0
Share

But as far as I understood that will only rotate my object around it's pivot and not around the center position...

avatar image hexagonius fred_gds · Feb 07, 2017 at 08:23 PM 0
Share

oh, right forgot about that.but then it really is easier to use a parent to the gameobject for rotation if possible.

Show more comments
avatar image igoraleftinovich · Aug 23, 2018 at 10:59 AM 1
Share

Hi! Did you found any solution?

avatar image eses igoraleftinovich · Aug 23, 2018 at 11:15 AM 0
Share

@igoraleftinovich - I think he was after aligning to ground while keeping heading... although maybe his object was floating, as he didn't want the base (of the object / feet) as orientation center, but the center of object... you'd be better off asking a new question I think, this question heading is really vague, so no one will probably find it again...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tezemi · Aug 23, 2018 at 04:31 PM

Getting the true center of an object can be done using a collider attached to it, assuming the collider is proportional to the object itself. If that isn't the case, you could also use an object's sprite or mesh to get its true center. For example, if you have an object with a BoxCollider attached to it, you can use the method GetComponent<BoxCollider>().bounds.center to get the center of the box in world units. Then you can use the RotateAround method to rotate the object around it's true center. If you wish to only rotate around the y-axis, then it should be as simple as specifying that in the method's second argument. The following code has been tested with an object that has a wonky pivot point, but a BoxCollider2D that fits normally around the sprite:

 using UnityEngine;
 
 public class RotateAroundCenter : MonoBehaviour 
 {
     public float Speed = 20f;
 
     private void Update()
     {
          transform.RotateAround
          (
              GetComponent<BoxCollider2D>().bounds.center, 
              new Vector3(0f, 0f, 1f), 
              Speed
          );
     }
 }

So for your purposes, assuming you have a BoxCollider or something similar attached to your gameobject, you could do this in the update:

 transform.RotateAround
 (
     GetComponent<BoxCollider>().bounds.center, 
     new Vector3(0f, 1f, 0f), 
     Speed
 );

The first argument is the true center of your object, the second argument is what axis you wish to rotate on, that being the y-axis, so you simply pass in a vector where the y value is one, and the x and z values are zero. Finally, your last argument is the angle you wish to rotate at. As long as this method is being called in Update(), FixedUpdate(), or something similar, then you should be able to achieve a constantly rotating object by just passing in the amount of degrees you wish to rotate each frame.

Comment
Add comment · Show 1 · 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 eses · Aug 23, 2018 at 07:36 PM 0
Share

@TryHardTrevor - Would this work in 3D? He wanted to align his object to ground, so that if you move uphill, top of the object will tilt backwards. He also mentioned that he wants to keep the heading ("need to keep my current rotation around the Y-axis"), so that while object adapts to ground surface, it will still look towards its previous heading. You seldom rotate sprites along Y, but this works for heading in 3D space ...so in this case he probably needs actually vector based solution where new rotation is based on ground normal and object forward. And finally, this rotation is applied and some offset to rotation point is done, so that rotation doesn't happen from transform pivot.

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

74 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

Related Questions

Transform angle to grid position 2 Answers

Unity3d find angle/direction of a Ray / Raycasthit 1 Answer

how to find direction between two points 2 Answers

Rotate an object using joystick 1 Answer

Angle to Direction 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