How can I smooth or damp euler rotations?

Hi all,

I’m not a programmer, I just have few 3D modelling skils, so please be patient with me. :slight_smile:
I’m modelling a dashboard in unity for a driving simulator. Everything is working fine, but what’s missing is a smooth rotation of the pointers (RPM and Speed). Actually they’re moving very harshly at a frequency of 200Hz.

Here’s the code:


//Calculating rotations
//RPM pointer (linear)
if ((float)data.vicrt_rpm <= 8000f)
	{
		RPM_rot 	= (float)data.vicrt_rpm * 259f / 8000f;
	}
	else
	{
		RPM_rot 	= 259f;
	}

//Speed pointer (non linear)
	
if ((float)data.vicrt_vel <= 0f)
	{
	speed_rot 	= 0f;
	}
else if ((float)data.vicrt_vel >0f && (float)data.vicrt_vel <= 100f)
	{
		speed_rot 	= (float)data.vicrt_vel * (130.5f/100f);
	}
else if ((float)data.vicrt_vel >100f && (float)data.vicrt_vel <= 180f)
	{
		speed_rot 	= 130.5f + ((float)data.vicrt_vel - 100f) * ((196f-130.5f)/(180f-100f));
	}
else if ((float)data.vicrt_vel >180f && (float)data.vicrt_vel < 300f)
	{
		speed_rot 	= 196f + ((float)data.vicrt_vel - 180f) * ((261f-196f)/(300f-180f));
	}
if ((float)data.vicrt_vel >= 300f)
	{
		speed_rot 	= 261f;
	}

//Applying rotations
RPM_gauge.transform.eulerAngles   = new Vector3 (270, 180, RPM_rot);
Speed_gauge.transform.eulerAngles = new Vector3 (270, 180, speed_rot);

What I need is to introduce some smoothing/damping in order to create a more realitic movement of the pointers.
I tried to use lerp, mathf, etc but with no results. Unfortunately I’ve almost zero skills with c# so that’s why maybe I wasn’t able to let them run.

Anyway, here’s a picture of the dashboard.

32887-snap_001.jpg

I’ll be grateful to whom will try to help me!

What you need to do is to set a max difference on the rotations, make those maximums frame independent, and apply them.

Say you want the RPM gauge to rotate at maximally 20 degrees per second. Then you need to do something along these lines in the end of your code:

//Applying rotations
float oldRPM_rot = RPM_gauge.transform.eulerAngles.Z;
float diff = RPM_rot - oldRPM_rot;

/* if the difference is greater than the max limit of 20 degrees per second, 
 * scale it back to that maximum 
 */
if(Mathf.Abs(diff) > 20 * Time.deltaTime)
    RPM_rot = oldRPM_rot + (20 * Time.deltaTime * Mathf.Sign(diff));

RPM_gauge.transform.eulerAngles = new Vector3 (270, 180, RPM_rot);

Two notes on this:

  1. This is assuming you’re in Update. If you’re using FixedUpdate, use Time.fixedDeltaTime
  2. The Sign-function gives 1 or -1, depending on the sign of the argument.

Dear Baste, I’ve managed this way:

		diff = RPM_rot - oldRPM_rot;

		RPM_rot = oldRPM_rot + (Mathf.Abs(diff)* speed_gauge * Time.deltaTime * Mathf.Sign(diff));

		oldRPM_rot = RPM_rot;

This way it works great. I made it dependent on the difference and looks like it has eliminated any vibration of the pointer.

Thanks again!