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
2
Question by CookieKirby · Nov 27, 2013 at 10:46 PM · orbitplanetlookrotationlookattarget

How can I make a gameobject "orbit" another

I am trying to make one game object orbit a star by having a rigidbody look at the star and the planet itself is a child of the rigidbody. heres my code:

 using UnityEngine;
 using System.Collections;
 
 public class Orbit : MonoBehaviour {
 
     public Vector3 Force;
     public Transform Target;
     
     void FixedUpdate()
     {
         transform.LookAt(Target);
         rigidbody.AddRelativeForce(Force);
     }
 }

the Problem is that when i use this, the rigidbody looks at the star, but it does not move sideways relatively so that it will "orbit", it simply moves sideways without turning. How can i fix this?

Comment
Add comment · Show 2
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 OP_toss · Nov 28, 2013 at 12:50 AM 0
Share

Hmmm many questions...

To simply orbit, you could create an empty game object GO, place the planet as a child of GO, with a position offset in one dimension equal to your radius of orbit, and place the GO under the star at 0,0,0. Then by rotating GO you "orbit" the planet. This is the fastest and cleanest solution, but may not be ideal for your use.

Otherwise you can just use math to move it in a cyclical pattern around the star's position, and call lookat every update. Not as fast, more code, but avoids potentially unwanted parenting.

Hope this helps!

avatar image CookieKirby · Nov 28, 2013 at 03:25 AM 0
Share

the planet "hops" around in a circular fashion, how can i fix it

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by AlejandroGorgal · Nov 28, 2013 at 01:03 AM

You can use a Slerp for that, here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class GravityScript : MonoBehaviour 
 {
     public Transform target;
     
     
     void Update () 
     {
         Vector3 relativePos = (target.position + new Vector3(0, 1.5f, 0)) - transform.position;
         Quaternion rotation = Quaternion.LookRotation(relativePos);
         
         Quaternion current = transform.localRotation;
         
         transform.localRotation = Quaternion.Slerp(current, rotation, Time.deltaTime);
         transform.Translate(0, 0, 3 * Time.deltaTime);
     }
 }

And here's the tutorial that explains how to use it: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/quaternions

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 Judgeking · Apr 05, 2020 at 11:33 AM 0
Share

Updated URL to tutorial: https://learn.unity.com/tutorial/quaternions

avatar image
2

Answer by numberkruncher · Nov 28, 2013 at 02:31 AM

Not sure if this would achieve what you want, but the easy option might be to:

  1. Create the following object hierarchy:

      Star
           |--- Planet
    
    
  2. Position the planet relative to the star at desired distance.

  3. Create a script which simply rotates Star since Planet will appear to orbit.

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 vargata · Nov 28, 2013 at 02:35 AM

here is a little sample code. if you add a rigidbody to your object and play with drags you can set up a pretty orbit. would be better modelling the forces

 #pragma strict
 
 var target : Transform;

 private var dir : Vector3;
 private var forward : Vector3;
 private var side : Vector3;
 private var cross : Vector3;
 
 function Start () {
     forward = transform.forward; //forward vector just to have it (in 3d you need a plane to calculate normalvector, this will be one side of the plane)
     dir = target.transform.position - transform.position; //direction from your object towards the target object what you will orbit (the other side of the plane)
     side = Vector3.Cross(dir, forward); //90 degrees (normalvector) to the plane closed by the forward and the dir vectors
 }
 
 function Update () {
     dir = target.transform.position - transform.position;
     transform.LookAt(dir.normalized); // look at the target
     rigidbody.AddForce(dir.normalized * 50);//add gravity like force pulling your object towards the target
     cross = Vector3.Cross(dir, side); //90 degrees vector to the initial sideway vector (orbit direction) /I use it to make the object orbit horizontal not vertical as the vertical lookatmatrix is flawed/
     rigidbody.AddForce(cross.normalized * 50); //add the force to make your object move (orbit)
 }

you need the drag value to work against these forces or your object will fly away on a spiral the bigger the drag the smaller the orbit range

Comment
Add comment · Show 4 · 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 CookieKirby · Nov 28, 2013 at 04:19 AM 0
Share

what could i set the Variables to to make a nice orbit? my planet is 21 meters away from the star. The star is 5 meters in diameter and the planet is 1

avatar image vargata · Nov 28, 2013 at 06:16 AM 0
Share

well, i don't know, i'm new to unity, i just put 10 as mass and 0.1 to drag. but my planets size is 60 and 100. :) that made a pretty orbit for me

avatar image CookieKirby · Nov 29, 2013 at 02:58 AM 0
Share

I mean the Vector3's

avatar image vargata · Nov 29, 2013 at 03:04 PM 0
Share

you dont have to give them a value, they are set up in the Start function.

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

21 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

Related Questions

Simple Planet orbit 3 Answers

How to predict orbit based on time? 1 Answer

LookAt doesnt work with cardboard SDK or any camera movement 0 Answers

Planet shadow flicker and shake when orbiting their star. 0 Answers

Axial tilt and moons 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