using UnityEngine;
using System.Collections;

public class RockBehaviour : MonoBehaviour {
    public float smooth = 1.0F;
    public float targetTiltL = 90.0F;
    public float closeEnough = 0.5F;

    private const float MAX_ROT = 360.0F;
    public float targetTilt, targetTiltR, targetR;

    void Start()
    {
        targetTilt = targetTiltL;
        targetR = MAX_ROT - targetTiltL;
        targetTiltR = -targetTiltL;
    }

    void Update()
    {
        Quaternion target = Quaternion.Euler(targetTilt, 0, 0);
        transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);

        float eulerX = transform.rotation.eulerAngles.x;
        if(targetTilt == targetTiltL)
        {
            if (eulerX < 180 && eulerX >= (targetTiltL - closeEnough))
            {
                targetTilt = targetTiltR;
            }
        }
        else
        {
            if (eulerX > 180 && eulerX <= (targetR + closeEnough))
            {
                targetTilt = targetTiltL;
            }
        }
    }
}
