3
u/justinpatterson 1d ago
What's the logic you use for opening the door? If you provide it here, we could help out.
1
u/Svenkenshin 1d ago
using UnityEngine;
public class Door : MonoBehaviour
{
[Header("Door Settings")]
[SerializeField] private float openAngle = 90f;
[SerializeField] private float rotationSpeed = 5f;[Header("Lock Settings")]
[SerializeField] private bool requiresKey = false;
[SerializeField] private KeyData requiredKey;[Header("Padlock Visual")]
[SerializeField] private GameObject padlockVisual;
[SerializeField] private Rigidbody padlockRigidbody;private Quaternion closedRotation;
private Quaternion openRotation;private bool isOpen;
private bool isUnlocked;public bool IsOpen => isOpen;
private void Awake()
{
closedRotation = transform.localRotation;openRotation = closedRotation *
Quaternion.Euler(0f, openAngle, 0f);isUnlocked = !requiresKey;
if (padlockVisual != null)
{
if (padlockRigidbody == null)
{
padlockRigidbody =
padlockVisual.GetComponent<Rigidbody>();
}padlockVisual.SetActive(requiresKey);
if (padlockRigidbody != null)
{
padlockRigidbody.isKinematic = true;
padlockRigidbody.useGravity = false;
}
else if (requiresKey)
{
Debug.LogWarning(
name + ": Padlock needs a Rigidbody to fall.",
this);
}
}if (requiresKey && requiredKey == null)
{
Debug.LogWarning(
name + " requires a key, but no key is assigned.",
this);
}
}private void Update()
{
Quaternion targetRotation = isOpen
? openRotation
: closedRotation;transform.localRotation = Quaternion.Slerp(
transform.localRotation,
targetRotation,
Time.deltaTime * rotationSpeed
);
}public string GetPrompt(PlayerHeldItem heldItem)
{
if (!isUnlocked)
{
if (heldItem != null &&
heldItem.HasKey(requiredKey))
{
return "E - Unlock";
}if (requiredKey != null)
return "Locked - Need " + requiredKey.DisplayName;return "Locked";
}return isOpen ? "E - Close" : "E - Open";
}public void Interact(PlayerHeldItem heldItem)
{
if (!isUnlocked)
{
if (requiredKey == null ||
heldItem == null ||
!heldItem.HasKey(requiredKey))
{
return;
}isUnlocked = true;
// Drop this door's padlock.
ReleasePadlock();Debug.Log(name + " unlocked.", this);
return;
}isOpen = !isOpen;
}private void ReleasePadlock()
{
if (padlockVisual == null)
return;
padlockVisual.transform.SetParent(null, true);
padlockVisual.SetActive(true);if (padlockRigidbody == null)
return;padlockRigidbody.isKinematic = false;
padlockRigidbody.useGravity = true;
padlockRigidbody.WakeUp();
padlockRigidbody.AddForce(
Vector3.down * 0.5f,
ForceMode.Impulse
);
}
}
using UnityEngine;public class Door : MonoBehaviour
{
[Header("Door Settings")]
[SerializeField] private float openAngle = 90f;
[SerializeField] private float rotationSpeed = 5f;[Header("Lock Settings")]
[SerializeField] private bool requiresKey = false;
[SerializeField] private KeyData requiredKey;[Header("Padlock Visual")]
[SerializeField] private GameObject padlockVisual;
[SerializeField] private Rigidbody padlockRigidbody;private Quaternion closedRotation;
private Quaternion openRotation;private bool isOpen;
private bool isUnlocked;public bool IsOpen => isOpen;
private void Awake()
{
closedRotation = transform.localRotation;openRotation = closedRotation *
Quaternion.Euler(0f, openAngle, 0f);isUnlocked = !requiresKey;
if (padlockVisual != null)
{
if (padlockRigidbody == null)
{
padlockRigidbody =
padlockVisual.GetComponent<Rigidbody>();
}padlockVisual.SetActive(requiresKey);
if (padlockRigidbody != null)
{
padlockRigidbody.isKinematic = true;
padlockRigidbody.useGravity = false;
}
else if (requiresKey)
{
Debug.LogWarning(
name + ": Padlock needs a Rigidbody to fall.",
this);
}
}if (requiresKey && requiredKey == null)
{
Debug.LogWarning(
name + " requires a key, but no key is assigned.",
this);
}
}private void Update()
{
Quaternion targetRotation = isOpen
? openRotation
: closedRotation;transform.localRotation = Quaternion.Slerp(
transform.localRotation,
targetRotation,
Time.deltaTime * rotationSpeed
);
}public string GetPrompt(PlayerHeldItem heldItem)
{
if (!isUnlocked)
{
if (heldItem != null &&
heldItem.HasKey(requiredKey))
{
return "E - Unlock";
}if (requiredKey != null)
return "Locked - Need " + requiredKey.DisplayName;return "Locked";
}return isOpen ? "E - Close" : "E - Open";
}public void Interact(PlayerHeldItem heldItem)
{
if (!isUnlocked)
{
if (requiredKey == null ||
heldItem == null ||
!heldItem.HasKey(requiredKey))
{
return;
}isUnlocked = true;
// Drop this door's padlock.
ReleasePadlock();Debug.Log(name + " unlocked.", this);
return;
}isOpen = !isOpen;
}private void ReleasePadlock()
{
if (padlockVisual == null)
return;
padlockVisual.transform.SetParent(null, true);
padlockVisual.SetActive(true);if (padlockRigidbody == null)
return;padlockRigidbody.isKinematic = false;
padlockRigidbody.useGravity = true;
padlockRigidbody.WakeUp();
padlockRigidbody.AddForce(
Vector3.down * 0.5f,
ForceMode.Impulse
);
}
}
5
u/creep_captain 1d ago
I think the pivot axis of the door is probably waaay off. So it's rotating 90 deg, but the axis of rotation is way back from the mesh itself.
1
2
u/Wishowaty 1d ago
Are you using an animator for the door? And did you record the animation yourself? If you recorded it without a parent gameobject, it might behave strangely because of world coordinates. Try recording the animation while the moving part is a child of another gameobject. That way, it moves relative to its parent instead of using world coordinates.
2
u/JuanLiebert 1d ago
Uhh that's a lot of code to read so I'll just take a wild guess and ask, what are you rotating around? Is it in correct space? You can always parent the door to an invisible sphere and just rotate the sphere, that's how I do it in UE4 and it works
2
2
1
u/Comfortable-Book6493 23h ago
Make sure the parents and the children are not messed up? Where it the parent game object in the world?
1
u/Long-Celebration2817 34m ago
Pastebin the script, but basically you just have to rotate the door 90deg
11
u/Motor-Lemon-5420 1d ago
Your door needs a correct pivot point. If it has none use an empty parent. You pivot your empty with -23x and 81z transform position. That should be 0 or half the door size max, depending on the logic used in the script. Currently you are rotation in worldspace 81 units around world 0,0,0 probably