Rotate an object 90 degrees on a button press

Hey guys,

Recently I started doing some coding in Unity and up until now everything has been going great, but I’m stuck now.

Basically this here is my code right now:

using UnityEngine;
using System.Collections;

public class InventorySystem : MonoBehaviour {

	private bool inInventory = false;
	[SerializeField]
	private GameObject[] inventoryitems;
	[SerializeField]
	private GameObject spot1;
	[SerializeField]
	private GameObject turnpoint;
	private MeshRenderer mr;
	private float horizontal = 0.0f;
	[SerializeField]
	private float damp;

	void Start () {

	}

	// Update is called once per frame
	void Update () {
	
		if (Input.GetButtonDown ("Inventory")) {
			
			if (inInventory == false) {
				inInventory = true;
			} else if (inInventory == true) {
				inInventory = false;
			}
			
		}

		if (inInventory == false) {

			for (int i = 0; i < inventoryitems.Length; i++) {
				mr = inventoryitems*.GetComponent<MeshRenderer> ();*
  •  		mr.enabled = false;*
    
  •  	}*
    
  •  } else if (inInventory == true) {*
    
  •  	for (int i = 0; i < inventoryitems.Length; i++) {*
    

_ mr = inventoryitems*.GetComponent ();_
_
mr.enabled = true;_
_
}*_

* }*

* if (inInventory == true) {*

* horizontal = Input.GetAxis(“Horizontal”);*

* if(horizontal >= 0.1){*

* turnpoint.transform.Rotate(0, 90, 0);*

* } else if (horizontal >= -0.1){*

* }*

* }*

* }*
}

Right now when I press a button the if (horizontal > 0.1) becomes true so it runs it and it then very snappily and instantly rotates a certain object, however what my goal is is for this to be a smooth rotation where you can see the object slowly rotation when the button is pressed.
I’ve already tried replacing the turnpoint.transform(0,90,0); line by instead using a lerp, but the problem there is that it only rotates when I keep holding the button, what I want to do is to be able to press the button for only a fraction of a second and it should just finish up the 90 degree turn, then afterwards if I press the button again it should still be able to rotate another 90 degrees the same way.
I hope you guys can help, thanks already!
EDIT: Thanks for the answer Yoeyyutch, your code really helped me figure out a lot of what I was doing. A friend of mine and I took you code and looked at mine and applied the same principle you used, however we ended up making it a bit more complicated (and slightly sloppy, I still need to refactor this code so it’ll be a bit cleaner, but for now it works).
For anyone interested here is the code I ended up using:
using UnityEngine;
using System.Collections;

public class InventorySystem : MonoBehaviour {

* private bool inInventory = false;*
* [SerializeField]*
* private GameObject[] inventoryitems;*
* [SerializeField]*
* private GameObject spot1;*
* [SerializeField]*
* private GameObject turnpoint;*
* private MeshRenderer mr;*
* private float horizontal = 0.0f;*
* [SerializeField]*
* private float damp;*
* private Quaternion rotationGoal;*
* private bool leftrightswitch = false;*

* void Start () {*

* }*

* // Update is called once per frame*
* void Update () {*

* if (Input.GetButtonDown (“Inventory”)) {*

* if (inInventory == false) {*
* inInventory = true;*
* } else if (inInventory == true) {*
* inInventory = false;*
* }*

* }*

* if (inInventory == false) {*

* for (int i = 0; i < inventoryitems.Length; i++) {*
_ mr = inventoryitems*.GetComponent ();
mr.enabled = false;
}*_

* } else if (inInventory == true) {*

* for (int i = 0; i < inventoryitems.Length; i++) {*
_ mr = inventoryitems*.GetComponent ();
mr.enabled = true;
}*_

* }*

* if (inInventory == true) {*

* horizontal = Input.GetAxis(“Horizontal”);*

* if(horizontal >= 0.1){*

* Quaternion rotationGoal = new Quaternion();*
* rotationGoal = Quaternion.Euler(turnpoint.transform.localRotation.eulerAngles.x, turnpoint.transform.localRotation.eulerAngles.y + 90, turnpoint.transform.localRotation.eulerAngles.z);*
_ turnpoint.transform.localRotation = Quaternion.Slerp(turnpoint.transform.localRotation, rotationGoal, damp * Time.deltaTime);
* leftrightswitch = true;*_

* } else if (horizontal <= -0.1){*

* Quaternion rotationGoal = new Quaternion();*
* rotationGoal = Quaternion.Euler(turnpoint.transform.localRotation.eulerAngles.x, turnpoint.transform.localRotation.eulerAngles.y - 90, turnpoint.transform.localRotation.eulerAngles.z);*
_ turnpoint.transform.localRotation = Quaternion.Slerp(turnpoint.transform.localRotation, rotationGoal, damp * Time.deltaTime);
* leftrightswitch = false;*_

* } else {*

* if(leftrightswitch == true){*

* if(turnpoint.transform.localRotation.eulerAngles.y < 90){*

* Quaternion rotationGoal = new Quaternion();*
* rotationGoal = Quaternion.Euler(turnpoint.transform.localRotation.eulerAngles.x, 90, turnpoint.transform.localRotation.eulerAngles.z);*
_ turnpoint.transform.localRotation = Quaternion.Lerp(turnpoint.transform.localRotation, rotationGoal, damp * Time.deltaTime);_

* } else if (turnpoint.transform.localRotation.eulerAngles.y < 180){*

* Quaternion rotationGoal = new Quaternion();*
* rotationGoal = Quaternion.Euler(turnpoint.transform.localRotation.eulerAngles.x, 180, turnpoint.transform.localRotation.eulerAngles.z);*
_ turnpoint.transform.localRotation = Quaternion.Lerp(turnpoint.transform.localRotation, rotationGoal, damp * Time.deltaTime);_

* } else if (turnpoint.transform.localRotation.eulerAngles.y < 270){*

* Quaternion rotationGoal = new Quaternion();*
* rotationGoal = Quaternion.Euler(turnpoint.transform.localRotation.eulerAngles.x, 270, turnpoint.transform.localRotation.eulerAngles.z);*
_ turnpoint.transform.localRotation = Quaternion.Lerp(turnpoint.transform.localRotation, rotationGoal, damp * Time.deltaTime);_

* } else {*

* Quaternion rotationGoal = new Quaternion();*
* rotationGoal = Quaternion.Euler(turnpoint.transform.localRotation.eulerAngles.x, 360, turnpoint.transform.localRotation.eulerAngles.z);*
_ turnpoint.transform.localRotation = Quaternion.Lerp(turnpoint.transform.localRotation, rotationGoal, damp * Time.deltaTime);_

* }*

* } else if(leftrightswitch == false){*

* if(turnpoint.transform.localRotation.eulerAngles.y > 90){*

* Quaternion rotationGoal = new Quaternion();*
* rotationGoal = Quaternion.Euler(turnpoint.transform.localRotation.eulerAngles.x, 90, turnpoint.transform.localRotation.eulerAngles.z);*
_ turnpoint.transform.localRotation = Quaternion.Lerp(turnpoint.transform.localRotation, rotationGoal, damp * Time.deltaTime);_

* } else if (turnpoint.transform.localRotation.eulerAngles.y > 180){*

* Quaternion rotationGoal = new Quaternion();*
* rotationGoal = Quaternion.Euler(turnpoint.transform.localRotation.eulerAngles.x, 180, turnpoint.transform.localRotation.eulerAngles.z);*
_ turnpoint.transform.localRotation = Quaternion.Lerp(turnpoint.transform.localRotation, rotationGoal, damp * Time.deltaTime);_

* } else if (turnpoint.transform.localRotation.eulerAngles.y > 270){*

* Quaternion rotationGoal = new Quaternion();*
* rotationGoal = Quaternion.Euler(turnpoint.transform.localRotation.eulerAngles.x, 270, turnpoint.transform.localRotation.eulerAngles.z);*
_ turnpoint.transform.localRotation = Quaternion.Lerp(turnpoint.transform.localRotation, rotationGoal, damp * Time.deltaTime);_

* } else {*

* Quaternion rotationGoal = new Quaternion();*
* rotationGoal = Quaternion.Euler(turnpoint.transform.localRotation.eulerAngles.x, 0, turnpoint.transform.localRotation.eulerAngles.z);*
_ turnpoint.transform.localRotation = Quaternion.Lerp(turnpoint.transform.localRotation, rotationGoal, damp * Time.deltaTime);_

* }*

* }*

* }*

* Debug.Log(turnpoint.transform.localRotation.eulerAngles.y);*

* }*

* }*
}
I hope this may help anyone else having a similar problem like me in the future!

using UnityEngine;
using System.Collections;

public class Rotate90 : MonoBehaviour 
{
	public float hZ =  50.00000f;
	float startAngle = 0.000000f;
	float finalAngle = 90.00000f;

	bool _keyPressed;
	bool _keyUp;
	bool _keyActive;
	bool _rotating;

	void Start () 
	{
		_keyPressed = false;
		_keyUp = false;
		_keyActive = true;
		_rotating = false;
	}
	
	void FixedUpdate () 
	{
		if(Input.GetKey(KeyCode.Space))
			_keyPressed = true;
		if(Input.GetKeyUp(KeyCode.Space))
			_keyUp = true;

		if(_keyPressed &&_keyUp)
		{	
			if(Mathf.Abs(startAngle)<finalAngle)
			{
				_rotating =true;
				if(_rotating)
					_keyActive=false;
				RotateMethod();
			}
			if(Mathf.Abs(startAngle)>=finalAngle)
			{
				_keyPressed = false;
				_keyUp = false;
				_keyActive = true;
				startAngle =0.00000f;

			}
		}
	}
	void RotateMethod()
	{
		transform.RotateAround(Vector3.zero, Vector3.up, hZ * Time.deltaTime);
		startAngle += hZ * Time.deltaTime;
	}

}