# How can I make smooth movement ?

Hi everyone,

I would like to ask you how can I make smooth object movements in Unity ?

In my scene I have a sun that changes its position based on values detected in real-time from Arduino, actually I’m using this function but it doesn’t work, because it’s change like a flash and not smoothly.

``````private void ReadValue(int[][] packageOfData, int chnIndex)
{
for (int i = 0; i < packageOfData.Length; i++)
{
if (packageOfData[i][chnIndex] < 1000 && packageOfData[i][chnIndex] > 800)
{
if (DirectionalLight != null)
{
DirectionalLight.transform.rotation = Quaternion.Slerp(point0.rotation, pointA.rotation, timeCount);
timeCount = timeCount + Time.deltaTime;
}
}
}
}
``````

Smoothing movement between any two particular values:

You have currentQuantity and desiredQuantity.

• only set desiredQuantity
• the code always moves currentQuantity towards desiredQuantity
• read currentQuantity for the smoothed value

Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.

The code: SmoothMovement.cs · GitHub

I don’t know enough about your project to answer this question but for slerp, your third parameter needs to a value between 0 and 1, so instead of just timeCount, you probably want timeCount divided by the total time (in seconds) it should take to get from point0.rotation to pointA.rotation.

1 Like

You mean that I have to change, instead of write down timeCount putting a value between 0 and 1 ?

The third parameter in any of Unity’s Lerping functions needs to be between 0 and 1. Anything greater than 1 will just be substituted for 1. Think about the third parameter of Slerp like a percentage. What percent between Point0 and PointA do you want your transform rotation to be at any given moment? if you pass in 0 as the third parameter then you’ll get Point0.rotation. If you pass 1 as the third parameter then you’ll get PointA.rotation. If you pass in 0.5 you’ll get half-way between Point0.rotation and PointA.rotation.

Assuming Point0 and PointA are fixed, then timeCount divided by the total time (as I mentioned previously) will give you a number between 0 and 1 that is proportional to the amount of the duration that has already passed. That’s just a guess based on common use-cases. I have no idea what point0.rotation and pointA.rotation represent.