Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
avatar image
0
Question by youngapprentice · Mar 13, 2015 at 02:45 AM · animationrotationphysics

Exact Rotation Around Another Point Over Time

Hi, all!

I am very familiar with rotating an object over time, and using Lerp to make the end result exact. However, I want to rotate an object around another point. I see two options here, and they both have issues. Feel free to help me with whatever you choose.

  1. I could use transform.RotateAround(), but this has no leaping abilities and often yields unreliable results in a while loop

  2. I could use animation, however after my object spins around its point and returns to its idle animation (Which is just a simple up and down 'floaty' location change over like 100 frames), it stays in its changed position, but rotates back to how it was before (I set no rotation keyframes in Blender so I don't know why this is happening).

These are my solutions. Please feel free to help with either as an answer, or if you have something better yourself feel free to share.

EDIT: This is the setup, with both ships parented to a sing empty in the middle. When a keystroke is made for rotating left or right, the proper ship should be used as a pivot and rotation should occur on the Z axis over 0.5-1.0 seconds. This complicates things because I don't think switching around parenting like that is good practice, especially later when the ships will interact with the world.

alt text

Thanks! - YA

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by youngapprentice · Mar 14, 2015 at 04:17 AM

Figured it out myself. Feel free to use the code if you'd like.

This code will rotate an object around a given pivot over a period of time and then snap to that value for absolute accuracy.

     IEnumerator SpinMe( Transform spinAxis, float degrees, float inTime ){
         coRunning = true;
         Vector3 firstAxisPos = spinAxis.position;
         Vector3 firstPos = transform.position;
         Quaternion firstRot = transform.rotation;
         Vector3 firstRotVector = transform.eulerAngles;
         float radius = Vector3.Distance( spinAxis.position, transform.position );
         float rads = degrees*Mathf.Deg2Rad;
         float yPos = radius*Mathf.Sin( rads );
         float xPos = radius*Mathf.Cos( rads );
         Vector3 newPos = new Vector3( xPos, yPos, firstPos.z );
         Vector3 byAngles = new Vector3( 0.0f, 0.0f, degrees );
         Quaternion newRot = Quaternion.Euler ( transform.eulerAngles + byAngles );
         spinning = true;
         float perc = 0.0f;
         for( float i = 0.0f; i < inTime; i += Time.smoothDeltaTime ){
             if( (i + Time.smoothDeltaTime ) >= inTime ) i = inTime;
             else{
                 perc = i/inTime;
                 perc = perc*perc*(3f-2f*perc);
             }
             transform.position = Vector3.Lerp ( firstPos, newPos, perc );
             transform.rotation = Quaternion.Lerp ( firstRot, newRot, perc );
             transform.position = Recalibrate ( firstAxisPos, spinAxis.position );
             yield return null;
         }
         Vector3 degreeChange = Vector3.forward*degrees;
         transform.Rotate( (firstRotVector+degreeChange)-transform.eulerAngles );
         transform.position = Recalibrate ( firstAxisPos, spinAxis.position );
         spinning = false;
 
         deltaRotation += degrees;
         if (Mathf.Abs ( deltaRotation ) == 180.0f) FlipMissiles();
 
     }
 
     Vector3 Recalibrate( Vector3 firstPos, Vector3 newPos ){
         float xMove = firstPos.x - newPos.x;
         float yMove = firstPos.y - newPos.y;
         return new Vector3( transform.position.x + xMove, transform.position.y + yMove, transform.position.z );
     }
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
avatar image
0

Answer by dagon · Mar 13, 2015 at 03:59 AM

create new GameObject have positon is the point(that you want rotate around).

Set your game object is new gameobject child.

rotate new game object by lerp function.

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 youngapprentice · Mar 13, 2015 at 11:24 AM 0
Share

I edited my answer to explain why this is not quite feasible. Thank you though.

avatar image
0

Answer by turtleburger · Mar 13, 2015 at 10:54 AM

Like dagon said, the simplest approach would be to set up a GameObject hierarchy to control this. So do something like:

PivotObject

  • ChildObject

'PivotObject' could then be rotated in any way and 'ChildObject' will follow. Place your rotation script on 'PivotObject' and you will most likely have better results with playing animations on 'ChildObject' without any transforms being overridden.

 using UnityEngine;
 public class RotateAroundPivot : MonoBehaviour 
 {
     public float speed;
 
     void Update()
     {
         // Use one or the other of these, not both! //
             
         // Rotation with Transform.RotateAround() //
         // transform.RotateAround(transform.position, transform.up, Time.deltaTime * speed);
 
         // Rotation with Quaternion.AngleAxis() //
         transform.rotation = Quaternion.AngleAxis(Time.deltaTime * speed, transform.up) * transform.rotation;
     }
 }


Try placing this script on PivotObject and commenting/uncommenting either the RotateAround() or AngleAxis() line. Either one should work just fine.

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 youngapprentice · Mar 13, 2015 at 11:23 AM 0
Share

Thanks for the in-depth answer, but I forgot to mention that this isn't quite a viable option, as the object I'm rotating is the parent of both pivots, and I want to target one of the children for a rotation.

The setup is like this: Two ships, both parented to an empty that sits in the middle. When a keystroke is made for rotating left or right, the proper ship should be used as a pivot and rotation should occur on the Z axis over 0.5-1.0 seconds. This complicates things because I don't think switching around parenting like that is good practice, especially later when the ships will interact with the world.

alt text

screen-shot-2015-03-13-at-71854-am.png (55.4 kB)

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

22 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

Related Questions

Character still walking when no key is already pressed 1 Answer

Physics not applying to the animated object after the animation ends ?! 0 Answers

Transform using custom editor doesn't recorded in animation 2 Answers

3D Space thruster force problem. 1 Answer

Question about pushing objects, "Animate Physics" and Rigidbodies 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