Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rorion · Jun 29, 2019 at 01:55 PM · unity 2dmovement scriptdiagonalellipse

How do I move an object along an ellipse path on diagonal?

Hi,

I need to move a game object along an ellipse path on any diagonal: upleft, upright, downleft or downright.

Now I'm getting only with directional movements: up, down, left or right.

For example:

             float positionX, positionY, centerX, centerY, semimajorAxes, semiminorAxes, alpha;
     
             void Start() {
                 positionX = 0f;
                 positionY = 0f;
                 centerX = 0f;
                 centerY = 0f;
                 alpha = 0f;
     
                 //going up
                 semimajorAxes = 1f;
                 semiminorAxes = 5f;
     
                 //going left
                 //semimajorAxes = 5f;
                 //semiminorAxes = 1f;
             }
     
             void Update() {
                 alpha += 1f;
     
                 positionX = centerX + (semimajorAxes * Mathf.Cos(Mathf.Deg2Rad * alpha));
                 positionY = centerY + (semiminorAxes * Mathf.Sin(Mathf.Deg2Rad * alpha));
     
                 transform.position = new Vector2(positionX, positionY);
             }

Do I need to change the formula? Any suggestion?

Thx!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 29, 2019 at 03:36 PM

You have essentially two ways to actually rotate your ellipse:

  • rotate your position around the center either through a rotation matrix or manually using sin and cos (which essentially would be the same thing)

  • Compose the final point by using pre-rotated unit coordinate axes.

Usually simpler is the second solution, especially if you use a tranform to define the coordinate space. Though even when you specify the axis yourself for the 2d case it's not that hard. First of all it gets way simpler when you actually use Vector2s for all your given information.

 public Vector2 center;
 public Vector2 radius;
 public float alpha;
 public Transform axis;
 
 void Update()
 {
     alpha += 20f * Time.deltaTime;
     float rad = alpha * Mathf.Deg2Rad;
     Vector2 p = new Vector2(Mathf.Cos(rad) * radius.x, Mathf.Sin(rad) * radius.y);
     transform.position = center + axis.right * p.x + axis.up * p.y;
 }

If you want to define the axes manually by a Vector2 you can do this:

 public Vector2 axis;
 
 // [ ... ]
 
 // replace the last line in Update with this:
 axis = axis.normalized;
 Vector2 up = new Vector2(-axis.y, axis.x);
 transform.position = center + axis * p.x + up * p.y;

Of course instead of calculating the normal (up vector) of our given axis you could define both axis yourself. However if the two axis are not perpendicular of each other the result would be a skewed ellipse.

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 Rorion · Jul 04, 2019 at 04:35 AM 0
Share

Thanks for the answers!

But in the first example, the object moves only from right to left in a straight line, not diagonally

And in the second example, the object does not move

:-(

avatar image Bunny83 Rorion · Jul 06, 2019 at 02:07 AM 0
Share

Are you sure you actuall set both values of the radius in the inspector to non zero values?

Also when you use the second example you also need to define a non zero axis. I normalize the axis in code, so you could set it to (1, 1) which would be 45°

avatar image
0

Answer by Rorion · Jul 16, 2019 at 12:25 AM

Thanks for the answers @Bunny83 !

But in the first example, the object moves only from right to left in a straight line, not diagonally

And in the second example, the object does not move :-(

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

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

112 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

Related Questions

Unity 2D movement and rotate sprite to the direction it is moving in 1 Answer

Player Movement doesn't work, but Debug.Log shows that it should 1 Answer

How do i make a cube move (Continuosly without stopping) when i press a button once in unity 2D 2 Answers

How to make slippery/smooth movement in Unity 2D? 3 Answers

Why wont my attack script get the data from my movement script (error)? 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