- Home /

# Moving an object in elliptical path - but with change in scale when on top half and bottom half

I have a sphere in my game which I want moving an elliptical path. But, I want it to change the scale of the ball according to where it is located in its elliptical trajectory. For an accurate depiction, please see the picture attached. Here is the code so far - which basically animates the ball in an elliptical trajectory. How can I incorporate the smooth change in the scale of the sphere as per the trajectory in the following code?

```
public float alpha = 0f; //Speed
public float tilt = 0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
transform.position = new Vector2(0f + (50f * MCos(alpha) * MCos(tilt)) - (50f * MSin(alpha) * MSin(tilt)), 0f + (40f * MCos(alpha) * MSin(tilt)) + (40f * MSin(alpha) * MCos(tilt)));
alpha += 1.5f;
}
float MCos(float value)
{
return Mathf.Cos(Mathf.Deg2Rad * value);
}
float MSin(float value)
{
return Mathf.Sin(Mathf.Deg2Rad * value);
}
```

[1]: /storage/temp/144205-ball-movement.png

**Answer** by Captain_Pineapple
·
Aug 07, 2019 at 11:14 AM

Hey there,

2 options:

go by the distance from the center:

A = minimum distance from the center

B = maximum distance from the center

Scale should then be something like:

Scale = minimumScale + (currentCenterDistance - A)/(B-A)*maximumScaleDifference

alternatively you could go with a sinus/cosinus approach:

Scale = minimumScale + maximumScaleDifference*sinus(alpha + tilt)

(perhaps the last line should be cosinus instead of sinus, depends on where your starting point is)

Hope this helps you, if you have questions on this let me know.

Hi,

If I understood it correctly, wouldn't A i.e. minimum distance from the center be (Length of minor axis/2) and maximum distance be (Length of major axis/2) ?

Also, what do you mean by "maximumScaleDifference"?

Moreover, computing currentCenterDistance from a point on the ellipse to its center does not seem to be trivial.

Hey there,

assuming i understood correctly that the maximum scale should be 5 and the minimum is 1 then the maximumScale difference would be 4.

Yes the distance would be half the corresponding axis as you posted.

How come calculating the current distance is difficult? As far as i can see the center of your ellipse is (0,0) ? So then the distance would be `transform.position.magnitude`

. In case the center is some other point: `distance = (center - transform.position).magnitude`

Hi Captain_Pineapple,

Ah okay. I had thought I had to use math formulas using the equations of an ellipse.

Meanwhile, I tried your second approach using Sin/Cosine as a function of alpha - but it seems that the scale gets increasingly positive values in the +ve Y axis i.e when the sphere is in the top-half trajectory. And negative values in the bottom-half trajectory! Also, this formula produces a pattern where the sphere has maximum scale at the sides (if Cos is used) or at the top⊥ (if Sin is used).

But in my case, I want the sphere to be at its lowest scale when its at the top (scale = 1), then move say - anticlockwise, and increase the scale linearly to 5 when its at the left, then move to the bottom where it will be at the highest scale (10) - then again move to the right where it will decrease to 5. Finally back to the top where it will decrease to 1. And so this cycle continues.

So, the scale of the sphere at the left and right-side will be the same i.e. 5 but the scale at the top and bottom will be opposite i.e. at the top only 1 but at the bottom 10.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Distribute terrain in zones 3 Answers

Connecting two spheres with a cylinder 1 Answer

Multiple Cars not working 1 Answer

Animated camera problems 0 Answers

The scene is opposite when in play mode,The scene moves from one side to other 0 Answers