How can I rotate an object 180 degrees? (C#)

I’m unsure what code to use for this. I want to be able to rotate an object 180 degrees on the Y axes smoothly.

I don’t know if I should be using transform.eulerAngles or Quaternion.Lerp. I don’t even know the difference.

I don’t need anything too complicated, it just has to be able to make the rotation.

Rotation Y axis instantly:

transform.RotateAround (transform.position, transform.up, 180f);

Rotation Y axis over time:

transform.Rotate(0, Time.deltaTime * 30, 0, Space.Self);

You can use Vector3.Lerp to transition from your current euler angles to a set of target angles 180 degrees around the y axis (Vector3.up).

public float smooth = 1f;

private Vector3 targetAngles;

void Update () 
{
	if(Input.GetKeyDown(KeyCode.S)) // some condition to rotate 180
		targetAngles = transform.eulerAngles + 180f * Vector3.up; // what the new angles should be
	
	transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetAngles, smooth * Time.deltaTime); // lerp to new angles
}

Just so you know, transform.eulerAngles is essentially the angles X, Y, and Z that appear under transform in the inspector.

You can also use Quaternions to do the same thing. It’s neater in code but slightly more performance costly. You can set the target rotation to the opposite direction you’re looking.

public float smooth = 1f;

private Quaternion targetRotation;

void Update () 
{
	if(Input.GetKeyDown(KeyCode.S)) // some condition
		targetRotation = Quaternion.LookRotation(-transform.forward, Vector3.up);
	
	transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, smooth * Time.deltaTime);
}

Hope this helps.

public float hight=5;
private float spincount;
private float jumpCount;
private float degree;
private float spinspeed;
void Start () {
jumpCount = 0;
spincount=1;
degree = 360;
spinspeed = 6;
}
void Update() {
//Jumping
if(Input.GetKeyDown (KeyCode.Space)) {
rigi.velocity = new Vector3 (0, hight, 0);
jumpCount = 1;
}
//Rotating
if (jumpCount == 1 && spincount < degree/spinspeed) {
transform.Rotate (Vector3.back*spinspeed);
spincount += 1;
} else {
jumpCount = 0;
spincount = 0;
}
}
}

Here you go, my whole script for a jumping cube rotating an exact degree smoothly. Just modifiy it for your needs. It tried everything else too, but this was the only one which really works reliably even while jumping.