• 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
Question by $$anonymous$$ · Apr 15, 2018 at 02:15 AM · rotationvector3quaternionmathlookat

Rotate an object so its up vector matches a sphere

Hi all,

I currently have an object rotating around a sphere continuously, however, I want the object to align with the sphere and the direction it's going.
For example, I have this spaceship rotating around a planet:
https://i.imgur.com/xZ0CwjA.png

However, it always faces the same direction.
My desired result is something like this:
https://i.imgur.com/fxOQ4AW.png
The spaceships bottom faces towards the sphere, and the spaceship looks the direction of travel.

If anyone could help, that'd be great!

Comment

People who like this

0 Show 0
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
Best Answer

Answer by MarioSantoso · Apr 15, 2018 at 05:33 AM

Apply this script to your ship/shuttle. Do not use rigidbody as we are moving the object manually not using physics.

The ship z axis must be forward and y axis must be up.

Make modifications as you see fit

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Orbit : MonoBehaviour
 {
     public Transform orbitTarget; // Assign the sphere/planet you want to orbit
     public float speed; // how fast the ship is moving
     public float distance; // how far the ship will orbit
     public bool updateDistance; // check this if you make changes to distance so the dummy's position will be updated
 
     private Transform _directionDummy;
 
     private void Start()
     {
         InitializeDummy();
     }
 
     private void InitializeDummy()
     {
         // Create a dummy object for the ship to look at, and position the dummy accordingly
         if (_directionDummy == null)
         {
             _directionDummy = new GameObject("_dummy").GetComponent<Transform>();
             _directionDummy.parent = orbitTarget;
         }
 
         _directionDummy.position = orbitTarget.position;
         _directionDummy.rotation = Quaternion.identity;
         _directionDummy.localScale = Vector3.one;
         _directionDummy.Translate(new Vector3(0, 0, -distance));
     }
 
     private void Update()
     {
         UpdateDummy();
 
         // Move the ship by following the dummy object and make it bottom side always face the sphere
         Vector3 _worldUp = transform.position - orbitTarget.position;
         transform.LookAt(_directionDummy, _worldUp * 2);
         transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.Self);
 
         if (Time.frameCount % 30 == 0)
             Debug.Log(Vector3.Distance(transform.position, _directionDummy.position));
     }
 
     private void UpdateDummy()
     {
         // Move the dummy around the sphere
         if (updateDistance)
         {
             updateDistance = false;
             Start();
         }
 
         _directionDummy.LookAt(orbitTarget, orbitTarget.up);
         _directionDummy.Translate(Vector3.right * speed * Time.deltaTime, Space.Self);
     }
 }
 
Comment

People who like this

0 Show 10 · 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 $$anonymous$$ · Apr 15, 2018 at 03:48 PM 0
Share

Hey thanks! However, I don't think this is fit for my use-case. All of my objects are created dynamically, and I believe this is a little more complicated for what exactly I'm trying to do.

I'm currently looking at the LookAt and I didn't realise I could give a vector that defines the up orientation. Is it possible to make the shuttle's bottom face the planet just using that?

avatar image MarioSantoso · Apr 15, 2018 at 10:47 PM 0
Share

This is not complicated at all since you don't have to worry about the mechanics, just assign an orbit target at set the orbit speed and distance.

If you created the object dynamically, then just add the orbit.cs to your object via script.

 AddComponenct<Orbit>();


Set the property by script also, for example:

 //Assign object to be your orbit target
 GetComponent<Orbit>().orbitTarget = "your planet transform";

The ship will then move by itself to the correct position. Do mind that I didn't put any smoothing to the movement.

avatar image $$anonymous$$ MarioSantoso · Apr 16, 2018 at 02:33 AM 0
Share

@Kilsnus
That's great, thank you. However one last issue - the object is very twitchy, its rotation keeps switching from looking ahead to looking towards the planet:
http://davidpuetter.com/u/%25pn-2018-04-16_03-32-08.mp4
This is seen with any other object too with the script attached.

avatar image MarioSantoso $$anonymous$$ · Apr 16, 2018 at 03:29 AM 0
Share

That's one hell twitchy ship! Haha.

From the test I did, it shouldn't be doing that unless a modification or another script interfering with the way it works.

If confirmed none of the above cause it, then I can only think that the ship is too close to the dummy object.


Let's test.... By default the dummy object spawned in front of the sphere. Try spawning or placing your ship further away from the sphere and not on the front of it. You can check the dummy position by selecting it in hierarchy and see the gizmo moving in the scene view.

If the ship and the dummy are very close together, you will get that glitch.

Show more comments
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

122 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

Related Questions

Calculate Quaternion.LookRotation manually 1 Answer

Rotate object based on hit normal + matching camera direction 0 Answers

Rotating wrong 0 Answers

Rotate towards target position (mouse) at a limited speed 1 Answer

Rotate player (rigidbody) towards his movement 2 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