First some context. My map is a tile based system with randomly generated walls and doors. All of these pieces are prefabs instantiated by script and placed in their proper location and orientation.
Right now I have doors. On an input key it fires a raycast that checks for the collision with objects on the Interactable layer. When one is found it runs a script on the door GameObject for it to rotate open.
They are currently rotating using a Quaternion.Slerp with a From of the current transform.rotation of the GameObject and the To of a transform.rotation of a public GameObject assigned to the tile prior to building.
This works fine, however it’s a hardcoded direction of rotation. What I am trying to do is determine a way to programatically create the destination Quaternion/Transform so that the door always rotates AWAY from the player. Right now it has a 50/50 chance of hitting the player in the face.
The info I have available is a RayCastHit, Player GameObject, Door GameObject. I have tried doing things like taking the ray.normal and flipping it 180 degrees, which gives a “forward” but that forward isn’t oriented up so it rotates the door correctly while also rotating it down into the floor.
I feel like this should be a lot simpler than I am making it. Anyone have thoughts?
private enum ActionStates { CLOSED, OPEN, CLOSING, OPENING, } private ActionStates actionState; private float timer = 0.0f; public Transform transOpen; private Transform transStart; public bool isUsable = true; public float timeMult = 0.5f; void Start () { actionState = ActionStates.CLOSED; transStart = transform; } void Update () { if (actionState == ActionStates.OPENING) { timer += Time.deltaTime * timeMult; transform.rotation = Quaternion.Slerp(transStart.rotation,transOpen.rotation, timer); if (timer >= 1.0f) { actionState = ActionStates.OPEN; } } public void DoUse() { if (actionState == ActionStates.CLOSED) { DoOpen(); } if (actionState == ActionStates.OPEN) { DoClose(); } } public void DoOpen() { actionState = ActionStates.OPENING; timer = 0.0f; } public void DoClose() { actionState = ActionStates.CLOSING; timer = 0.0f; }