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 Damson · Sep 28, 2013 at 05:14 PM · rotationmovementsphere

Moving an ob around a sphere

I would like to move an object (lets say a 2d triangle ) around a sphere. The following code results the triangle behave in a strange way.

 void Update () 
 {
     if (Input.GetAxis ("Horizontal") > 0)
     {
         abs_rotation += 0.1f;
     }
     if (Input.GetAxis ("Horizontal") < 0)
     {
         abs_rotation -= 0.1f;
     }
      if (Input.GetAxis ("Vertical") > 0)
     {
         this.rigidbody.AddForce(this.transform.forward * 10);
         Debug.Log("Up");
     }

     //I store the center of the sphere in a varriable
     Vector3 spherePos = GameObject.Find("Sphere").transform.position;

     //I calculate what would be the position of the object on the surface of the sphere
     //Distance represents the radius of the sphere

     Vector3 posVect = this.transform.position-spherePos;
     posVect.Normalize();
     posVect    *= DISTANCE;


     //I put my triangle to the right position and rotate i to the appropriate direction
     this.transform.position = posVect;
     this.transform.up = posVect;
     this.transform.RotateAround(posVect,abs_rotation);
 }

The problem is that my triangle behaves as if the bottom of the sphere was pulling it. Does anybody has any idea?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Sep 28, 2013 at 05:41 PM

Is this your actual code? It should not even compile, since RotateAround requires 3 parameters: the center of rotation, the axis about which to rotate and the angle. RotateAround modifies the position and rotation of the rotating object so that it keeps facing the center - modifying these properties while rotating isn't a good idea, and may cause the weird behaviour you're describing. Another point: Find'ing an object every Update is terribly slow: Find it once at Start and save its position in a member variable instead. The code for rotating the object about the sphere could be something like this:

 Vector3 center; // save the sphere center here

 void Start(){ // Find sphere only once:
     center = GameObject.Find("Sphere").transform.position;
 }

 void Update () 
 {
     if (Input.GetAxis ("Horizontal") > 0)
     {
         abs_rotation += 0.1f;
     }
     if (Input.GetAxis ("Horizontal") < 0)
     {
         abs_rotation -= 0.1f;
     }
     // rotate around sphere about world Y
     this.transform.RotateAround(center, Vector3.up, abs_rotation);
 }

Notice that abs_rotation is the rotation speed: this code rotates continuously around the sphere, and the left and right arrows just increase/decrease the speed. If you want to rotate only when these keys are pressed, change the code in Update to this:

 void Update () 
 {
     float rotAngle = Input.GetAxis ("Horizontal") * 0.1f;
     // rotate around sphere about world Y
     this.transform.RotateAround(center, Vector3.up, rotAngle);
 }

NOTE: I removed the Input.GetAxis("Vertical") code just because it was screwing up the formatting due to some mysterious and unknown reason - keep it in your code if you want.

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 Damson · Sep 28, 2013 at 06:14 PM 0
Share

First of all it compiles but I corrected the RotateAround function call as you suggested. If try the line "float rotAngle = Input.GetAxis ("Horizontal") * 0.1f;" nothing happens it doesn't rotate at all.

avatar image Damson · Sep 28, 2013 at 06:16 PM 0
Share

Sorry to be correct it rotates but only one unite an when I release the left or the right button it rotates back to the original direction automatically

avatar image aldonaletto · Sep 29, 2013 at 06:06 PM 0
Share

You're right about RotateAround: there's an obsolete and undocumented version that accepts 2 arguments (although I could not understand what exactly it does). I tested my scripts and both did what I expected: abs_rotation behaved like the rotation speed in the first one, while the second one made the object rotate around the sphere only when the horizontal keys were pressed - but it didn't return to the original direction when they were released. $$anonymous$$aybe something else in your script is restoring the original rotation.

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

16 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

Related Questions

Rigidbody.AddForce and incorrect rotation 2 Answers

Joystick controller problem walking on sphere 1 Answer

Can a ball rotate without moving and changing its axis? 1 Answer

Make sphere rotate when controlled 7 Answers

Third person follow camera on a sphere 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