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 cineboxandrew · Mar 14, 2013 at 06:11 AM · javascriptanglemathf.sinradialmathf.cos

How to have an object be plotted with an angle and altitude around an object

I have made a simple script to test the feasibility of this idea, but it doesnt work. #pragma strict var originX = 0; var originY = 0; var altitude = 100; var angle = 45; var relativeX = 0; var relativeY = 0; function Start () {

 }
 
 function Update () {
 if (angle < 90 && angle > 1) 
 {
     relativeX = Mathf.Sin(angle) * altitude;
     relativeY = Mathf.Cos(angle) * altitude;
     transform.position = Vector3(relativeX + originX,0,relativeY + originY);
 }
 else
 {
 angle = 0;
 }
 angle = angle + 0;
 
 }

I realize this wont work with angles outside 1 through 90, this was just a test, hence the check

the script will plot the object around the other, but it appears that every angle increment it adds 60°, and will somehow happily go above 90°, and well above 360°. the object also randomly disappears which i assume has something to do with bad scripting.

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 robertbu · Mar 14, 2013 at 06:18 AM 0
Share

I not clear what you are trying to do here. It looks like you want to plot a circular path around originX and originY. I don't see any code actually incrementing 'angle,' so I cannot figure out what is going wrong with that code. $$anonymous$$aybe a diagram and possibly more of your code would help.

avatar image cineboxandrew · Mar 14, 2013 at 06:20 AM 0
Share

i was wanting to plot an object with an angle and altitude around another object to create an "orbit" I realized I have been using degrees while mathf.sin() uses radians, but my ship is still disappearing randomly

1 Reply

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

Answer by robertbu · Mar 14, 2013 at 06:58 AM

First as for your code above, you have the Sin() and Cos() reversed for your polar to rectangular conversion. Here is your script with a few changes. Start with an empty scene and attach it to a cube:

 var speed = 0.001;
 var originX = 0.0;
 var originY = 0.0;
 var altitude = 4.0;
 var angle : float = 0.0;
 
 function Update () {
     var relativeX = Mathf.Cos(angle) * altitude;
     var relativeY = Mathf.Sin(angle) * altitude;; 
     angle = (angle + speed * Time.deltaTime) % (2.0 * Mathf.PI);
     transform.position = Vector3(relativeX + originX,0,relativeY + originY);
 }

Note that the are significantly easier ways to orbit object in Unity. One is to use Transform.RotateAround().

Another way:

  • Place an empty game object at the center of rotation.

  • Place your object to orbit in orbit at the right altitude.

  • Make the object in orbit a child of the empty game object by dragging and dropping it on the empty game object.

  • Rotate the empty game object with a simple script. Just call this each frame:

    transform.Rotate(Vector3(0,10 * Time.deltaTime, 0);

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 cineboxandrew · Mar 15, 2013 at 03:37 AM 0
Share

Thank you so much! what does the "Time.deltaTime) % (2.0 * $$anonymous$$athf.PI)" do? does that make higher orbits slower than lower ones?

avatar image robertbu · Mar 15, 2013 at 05:18 AM 0
Share

This code just increments the angle. By using using Time.deltaTime this way, I make the rotation independent of the frame rate, so that the rotation will happen at the same speed no matter what platform you run it on. The "% (2.0 $$anonymous$$athf.PI)" is probably not necessary. Angle is measured in radians which from 0 to 2 PI. This code just makes sure the angle is always in this range. It would almost certainly work without that code.

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

10 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

Related Questions

Field of view cone positioning? 0 Answers

Problem with Z rotation when rotating X and Y and the same time. 2 Answers

Setting a Jump Angle Relative to a Surface 1 Answer

How to cut an image at an angle from the center? 0 Answers

How do i make a flowing 90 degree turn? 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