Hi, folks. Noob here with first post. Thanks in advance for any guidance on offer.
I have a large cube with the Player inside.
The cube is comprised of six planes set as children of a parent empty gameobject named “Cubicle.”

I want the cube to rotate forward when the Player gets close (raycasting) to the Player-facing wall, so that the Player-facing wall becomes the floor. I’ve got it working with a poor implementation I hope can be improved. In its current form, as the Player I can approach any side wall and have it flip around to the floor. But instead of maintaining strict 90-degree intervals of rotation, variations creep in and accumulate to throw off the Player rotation – which should stay horizontal at all times.
Here’s a video showing the results of what I have so far.
You can see how if I (the Player) approach a wall at an angle, causing it to rotate to become the floor, the cube rotation goes askew. I need to somehow lock the rotation to multiples of 90 degrees to maintain the Player horizontal. I’ve scoured the net and forums for the right Vector/Quaternion approach but regretfully could not piece it together.
Here’s my rotation script that is attached to the Cubicle gameobject. (Ignore the nonsensical class name, which hasn’t changed since early testing.)
using UnityEngine;
using System.Collections;
public class CheckPosition : MonoBehaviour {
public float rayForwardDistance = 2;
public float lerpDuration = 1f;
private float currentLerpTime = 999;
private Quaternion startPosR, endPosR;
private GameObject cubicle, player;
private bool gotHitForward = false;
// Use this for initialization
void Start () {
cubicle = GameObject.Find("Cubicle");
player = GameObject.FindGameObjectWithTag("MainCamera");
if (player == null)
{
Debug.Log("Failed to locate object tagged 'MainCamera'");
}
InvokeRepeating("RayCastCheck", 0, 0.1f);
}
// Update is called once per frame
void Update () {
if (gotHitForward && currentLerpTime < lerpDuration)
{
currentLerpTime += Time.deltaTime;
//lerp!
float perc = currentLerpTime / lerpDuration;
transform.rotation = Quaternion.Slerp(startPosR, endPosR, perc);
}
}
void RayCastCheck ()
{
if (currentLerpTime < lerpDuration)
{
return; // currently lerping, so do nothing
}
RaycastHit hitForward;
gotHitForward = Physics.Raycast(player.transform.position, player.transform.forward, out hitForward, rayForwardDistance);
if (gotHitForward)
{
currentLerpTime = 0f;
GameObject theHitObject = hitForward.collider.gameObject;
Debug.Log("hitForward = " + theHitObject.name);
startPosR = transform.rotation;
endPosR = Quaternion.AngleAxis(90f, player.transform.right) * transform.rotation;
}
}
}