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 HammerBG · Apr 01, 2013 at 01:03 PM · rotationquaternionlookat

Lookat offset angle combined with RotateAround. Basically orbit + rot angle

Hello world!

I am having a bit of trouble with making a object orbit around another object on specific angle.

Basically what i have is this:

  transform.RotateAround(orbitAround.transform.position, Vector3.forward, 5 * Time.deltaTime);    
 transform.LookAt(orbitAround.transform.position, Vector3.back);

Which produces a working-ish result of this:

alt text

But what i was aiming for was more like this:

alt text

I am guessing there is a simple way to "lookat" an object with an offset angle of the looking object, but i cannot find how to do it.

Any help will be appreciated :)

Comment
Add comment · Show 4
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 · Apr 01, 2013 at 01:46 PM 0
Share

Place the ship in a correct position at the start, then remove the LookAt().

avatar image HammerBG · Apr 01, 2013 at 02:30 PM 0
Share

But how will then the object rotation change when it is in a different point on its orbit, since the ship is orbiting the planet?

avatar image robertbu · Apr 01, 2013 at 02:51 PM 0
Share

Just try it. For a simple orbit as you have here, it will work. If you decide to allow your ship to make arbitrary orbits around the planet, then you will have some work to do.

avatar image HammerBG · Apr 01, 2013 at 03:57 PM 0
Share

The thing is that the ship is able to break orbit and head for another planet/object, in which he can get into orbit again when he gets there. So i must be able to calculate the rotation offset at any given time. Which i am trying to figure out how to do. That or i didn't understand your suggestion :)

3 Replies

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

Answer by robertbu · Apr 01, 2013 at 04:32 PM

At the point you want to enter orbit you can get the look direction by the cross product between your axis of rotation, and a vector between the ship and the center of rotation. Something like (untested):

 var lookDir = Vector3.Cross(Vector3.forward, transform.position - orbitAround.transform.position);

You may have to reverse the parameters to the Vector3.Cross(). If you want to use Transform.LookAt(), you will need to add the base position of your ship to the look direction.

 transform.LookAt(lookDir + transform.Position);

This is the direction you would use if you were using Quaternion.SetLookRotation().

 transform.rotation = Quaternion.SetLookRotation(lookDir);

You may want to use SetLookRotation() as one step making an eased rotation based on a new look rotation. It depends on how you break and reestablish orbit.

For this to work, you also have to have your ship constructed so that the nose is facing positive 'Z'. My point above, is that once you've established the rotation of the ship, you don't have to set it each frame. RotateAround() will maintain the correct heading.

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 HammerBG · Apr 01, 2013 at 05:25 PM 0
Share

Again, thank you for helping me. Thanks to everyone else too, but Robert's answer got me the in the correct direction :)

transform.LookAt(this.owner.transform.position, Vector3.back); transform.Rotate(0, 90, 0);

This code got me the desired results. I also modified some of my code since i did changes to rotation on every update frame, which was obviously wrong, since i needed to do it only once. Thanks for clearing this one too Robert!

avatar image
0

Answer by instruct9r · Apr 01, 2013 at 02:44 PM

This happens because your ship's nose is aiming at the Z axis originally, which makes it look at the planet. What you can do is 2 options.

1: Make empty group and parent the ship in it. Reset ship's transformations and rotate the it so the X Axis of the group is where the ship's nose is aiming. Attach that script to the group, instead to the ship.

2: More simplified version i think is, just make one group, Position it at the center of the planet and then parent the ship inside. This way you will be able to rotate the ship as you want and just make the group to rotate around it's pivot on the desired axis :)

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 Owen-Reynolds · Apr 01, 2013 at 03:16 PM

The other answers might work better, but to directly answer your question, rotations are Quaternions, and are applied using the multiply symbol. A 90 degree-left spin is Quaternion.Euler(0,-90,0);.

So: transform.rotation = transform.rotation * Quaternion.Euler(0,-90,0); spins 90 left.

Tricky parts are: "times Q" will apply it as a local rotation, so will always spin to your current left. If the shuttle isn't facing "up" on it's orbit, that spin won't work Setting the 2nd LookAt parm to the rotation axis would make the shuttle spin "rotation up" while it looks at the earth. Or using "Q times" will use Q as a global rotation. Quaternion.AngleAxis lets you make a rotation around any axis.

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

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

12 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

Related Questions

How can I get the rotation between two objects 1 Answer

"Look rotation viewing vector is zero" error 2 Answers

Vector3.Angle making my object oscilate... 2 Answers

Rotating object to face target, while being rotated to match terrain slope 1 Answer

LookAt on only 1 axis for 2D games? 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