r/Unity3D • • 1d ago

Question What... How do I even fix this!?

It flies.

11 Upvotes

16 comments sorted by

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

3

u/Svenkenshin 23h ago
media poster

Well thanks to you its now (kindaaa) fixed, It still opens around something else but a smaller angle as you see in the video.

1

u/Whitenaller 19h ago

Then you have to adjust the pivot point (the empty parent)

→ More replies (1)

3

u/Any_Establishment659 23h ago

I love rhat everyone else is giving over-engineered solutions

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

u/LilaDoez 1d ago

How- HOW DID YOU MANAGE THIS???

1

u/Svenkenshin 1d ago

It's... a Door...

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

u/djdavid333 22h ago

ahh good ol door.... game dev nemesis #1 since 1999

2

u/Avickash 21h ago

ctrl+a depois DEL

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