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
3
Question by code-blep · May 26, 2013 at 10:51 PM · distancesmoothorbitradius

Smooth Orbit Round Object with Adjustable Orbit Radius

Hi,

I am trying to assign an object to rotate around another object and to be able to adjust the orbit radius. Also when the radius changes I am trying to get the object to smoothly adjust to the new orbit radius.

I've been searching for a while now and only had limited success. The best I have found is:

 rotateTransform.RotateAround (objectToOrbit.position, Vector3.up, orbitAmount * Time.deltaTime);

But this does not allow me to change the actual radius. The orbitAmount value simply speeds up or down the orbit.

It does seems that RotateAround works with the initial distance from the object I want to rotate around and sets it as that, but I can't think of a way to exploit that fact.

Any suggestions on how to go about this?

Thanks

Paul

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

5 Replies

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

Answer by robertbu · May 27, 2013 at 01:32 AM

Here is a bit of code:

 #pragma strict
 
 public var center : Transform;
 public var axis   : Vector3 = Vector3.up;
 public var radius = 2.0;
 public var radiusSpeed = 0.5;
 public var rotationSpeed = 80.0; 
 
 function Start() {
     transform.position = (transform.position - center.position).normalized * radius + center.position;
 }
 
 function Update() {
     transform.RotateAround (center.position, axis, rotationSpeed * Time.deltaTime);
     var desiredPosition = (transform.position - center.position).normalized * radius + center.position;
     transform.position = Vector3.MoveTowards(transform.position, desiredPosition, Time.deltaTime * radiusSpeed);
 }
Comment
Add comment · Show 3 · 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 code-blep · May 27, 2013 at 03:27 PM 0
Share

Perfect Roberbu! And I am sure the C# conversion will be appreciated by others Le$$anonymous$$oine :)

I'll be looking more into normalized today after seeing it in use like that.

Thanks guys!

avatar image code-blep · May 27, 2013 at 03:48 PM 0
Share

Just to share with everyone else, here is a modified version which makes the orbiting object face in the direction of travel, and to gradually move into the orbit from any position on start. I have also tried to implement good coding practice as I imagine people will just cut and paste this. Also just to clarify, I know that robertbu would have done the same, but obviously he was just throwing a quick example into the mix (thanks again robetbu!). Right here is the code:

 #pragma strict
  
 var objectToOrbit : Transform; //Object To Orbit
 var orbitAxis : Vector3 = Vector3.up; //Which vector to use for Orbit
 var orbitRadius : float = 75.0; //Orbit Radius
 var orbitRadiusCorrectionSpeed : float = 0.5; //How quickly the object moves to new position
 var orbitRoationSpeed : float = 10.0; //Speed Of Rotation arround object
 var orbitAlignToDirectionSpeed : float = 0.5; //Realign speed to direction of travel
 
 private var orbitDesiredPosition : Vector3;
 private var previousPosition : Vector3;
 private var relativePos : Vector3;
 private var rotation : Quaternion;
 private var thisTransform : Transform;
 
 //---------------------------------------------------------------------------------------------------------------------
 
 function Start() {    
     thisTransform = transform;
 }
 
 //---------------------------------------------------------------------------------------------------------------------
 
 function Update() {
     //$$anonymous$$ovement
     thisTransform.RotateAround (objectToOrbit.position, orbitAxis, orbitRoationSpeed * Time.deltaTime);
     orbitDesiredPosition = (thisTransform.position - objectToOrbit.position).normalized * orbitRadius + objectToOrbit.position;
     thisTransform.position = Vector3.Slerp(thisTransform.position, orbitDesiredPosition, Time.deltaTime * orbitRadiusCorrectionSpeed);
 
     //Rotation
     relativePos = thisTransform.position - previousPosition;
     rotation = Quaternion.LookRotation(relativePos);
     thisTransform.rotation = Quaternion.Slerp(thisTransform.rotation, rotation, orbitAlignToDirectionSpeed * Time.deltaTime);
     previousPosition = thisTransform.position;
 }
avatar image Piesk · May 01, 2015 at 05:14 PM 0
Share

@robertbu i don't really understand what the radiusSpeed is doing here. Is it simply making it lag from where it's supposed to be ?

The reason i ask is because i'm trying to make an object orbit an object that is orbiting something else (the moon orbiting the earth) and the position is not updating correctly. This post seems to have identified why.

I think if i remove the radiusSpeed then I have the solution I need?

avatar image
21

Answer by LeMoine · May 27, 2013 at 02:58 AM

Wow, works perfectly! here is in C#, tested with a cube as the center of rotation :

 using UnityEngine;
 using System.Collections;
 
 public class testRotate2 : MonoBehaviour {
     
     GameObject cube;
     public Transform center;
     public Vector3 axis = Vector3.up;
     public Vector3 desiredPosition;
     public float radius = 2.0f;
     public float radiusSpeed = 0.5f;
     public float rotationSpeed = 80.0f;
 
     void Start () {
         cube = GameObject.FindWithTag("Cube");
         center = cube.transform;
         transform.position = (transform.position - center.position).normalized * radius + center.position;
         radius = 2.0f;
     }
     
     void Update () {
         transform.RotateAround (center.position, axis, rotationSpeed * Time.deltaTime);
         desiredPosition = (transform.position - center.position).normalized * radius + center.position;
         transform.position = Vector3.MoveTowards(transform.position, desiredPosition, Time.deltaTime * radiusSpeed);
     }
 }

If I could thumb up, I would really!

Comment
Add comment · Show 3 · 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 LeMoine · May 27, 2013 at 11:18 AM 0
Share

Hmm, I think I need more $$anonymous$$arma to be able to close a topic. I can't see any checkmark next to the answers and I can't vote up your answer :'(

avatar image code-blep · May 27, 2013 at 11:54 AM 0
Share

Hi guys. It's my question lol! The answers look great and I will be checking them out in a few hours. As soon as I have I'll mark the question/answers accordingly. In the mean time have some up votes :)

avatar image altpage · Jan 24, 2018 at 04:31 PM 0
Share

Here is a face-towards-movement-direction-rotation if needed:

 Vector3 tangentVector = Quaternion.Euler(0, 90,0) * (transform.position -
  center.position);
 transform.rotation = Quaternion.LookRotation (tangentVector);
avatar image
0

Answer by aaronov · May 27, 2013 at 12:15 AM

You will probably need to use something like Mathf.SmoothStep to smooth the rotation angle argument of RotateAround and Transform.Translate to configure the distance (radius) between the two objects prior to applying rotation.

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 Celestium · Aug 21, 2013 at 12:07 AM 0
Share

using FixedUpdate seemed to smooth out some of the choppiness, at least for me.

avatar image
0

Answer by ssshake · Jan 06, 2014 at 06:25 AM

I noticed that the objects are jittery using this script but if you change update to fixedupdate it's smooth

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 deadshot · Oct 22, 2014 at 05:38 AM

 using UnityEngine;
  using UnityEngine;
  using System.Collections;
  
  //[AddComponentMenu("Camera-Control/Mouse drag Orbit with zoom")]
  public class rotateonmouse : MonoBehaviour
  {
      public Transform target;
      public float distance = 5.0f;
      public float xSpeed = 120.0f;
      public float ySpeed = 120.0f;
  
      public float yMinLimit = -20f;
      public float yMaxLimit = 80f;
  
      public float distanceMin = .5f;
      public float distanceMax = 15f;
  
      public float smoothTime = 2f;
  
      float rotationYAxis = 0.0f;
      float rotationXAxis = 0.0f;
  
      float velocityX = 0.0f;
      float velocityY = 0.0f;
  
      // Use this for initialization
      void Start()
      {
          Vector3 angles = transform.eulerAngles;
          rotationYAxis = angles.y;
          rotationXAxis = angles.x;
  
          // Make the rigid body not change rotation
          if (rigidbody)
          {
              rigidbody.freezeRotation = true;
          }
      }
  
      void LateUpdate()
      {
          if (target)
          {
              if (Input.GetMouseButton(1))
              {
                  velocityX += xSpeed * Input.GetAxis("Mouse X") * 0.02f;
                  velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
              }
  
              rotationYAxis += velocityX;
              rotationXAxis -= velocityY;
  
              rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
  
              Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
              Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
              Quaternion rotation = toRotation;
              
              
  
             
              Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
              Vector3 position = rotation * negDistance + target.position;
              
              transform.rotation = rotation;
              transform.position = position;
  
              velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
              velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
          }
  
      }
  
      public static float ClampAngle(float angle, float min, float max)
      {
          if (angle < -360F)
              angle += 360F;
          if (angle > 360F)
              angle -= 360F;
          return Mathf.Clamp(angle, min, max);
      }
  }
 
 this should do it. It allows to set the max/min distance and the distance you want.
Comment
Add comment · Show 2 · 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 OutcastOrange · Nov 12, 2015 at 07:03 PM 0
Share

I've implemented your code into a prototype I'm working on, and when I run it the camera goes zoo$$anonymous$$g away in the negative z direction. This also happens when I try to use other mouse orbit examples around here. Is there anything obvious that I might be doing wrong?

avatar image TokyoWarfareProject · Dec 05, 2018 at 04:43 PM 0
Share

fantastic script! best one out there!!

I've added distance control with mouse wheel, I've not added a new specific smooth value for distance but should be a breeze to add if needed. Really love your script.

link text

rotateonmouse.txt (3.0 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

How to get an object to follow another object at a set distance in 2D 3 Answers

Check Radius Problem 2 Answers

Math Question: Get EnemyObject to NavMeshDestination x Units from Player 1 Answer

How to make this script find distance inside radius? 1 Answer

Smooth transition for RotateAround 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