Hello as you know I’m doing the RPG course, and have a question out of the course I hope that’s ok?
I am in the process of building a door that is inside my sandbox.
For this I have bought a few assets in unity.
Now the problem every time I start the game the door automatically opens before I can even get near the collider and press T. I have set up the whole thing so that there is a collider at the door called a cube.
Here is the trigger script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.SceneManagment;
namespace RPG.SceneManagement{
public class DoorTrigger : MonoBehaviour
{
[SerializeField] private Door door;
private bool playerInside = false;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
playerInside = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
playerInside = false;
}
}
private void Update()
{
if (playerInside && Input.GetKeyDown(KeyCode.T))
{
door.OpenDoor();
}
}
}
}
Here is the door script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace RPG.SceneManagment
{
public class Door : MonoBehaviour
{
[SerializeField] private float openAngle = 90f;
[SerializeField] private float openDuration = 1f;
[SerializeField] private AnimationCurve openCurve = AnimationCurve.Linear(0f, 0f, 1f, 1f);
[SerializeField] private AudioClip openSound;
[SerializeField] private AudioClip closeSound;
[SerializeField] private Collider triggerCollider; // Der Collider des Würfels, der die Tür öffnet
private bool isOpen = false;
private Quaternion initialRotation;
private float openStartTime;
private AudioSource audioSource;
void Start()
{
triggerCollider = transform.Find("Cube").GetComponent<Collider>();
initialRotation = transform.rotation;
audioSource = GetComponent<AudioSource>();
isOpen = false; // Tür ist zu Beginn geschlossen
}
private void OnTriggerEnter(Collider other)
{
if (other == triggerCollider)
{
// Trigger nur auslösen, wenn die Tür noch geschlossen ist
if (!isOpen)
{
// Event auslösen
OnDoorTriggered?.Invoke();
// Tür öffnen
OpenDoor();
}
}
}
private void Update()
{
// If the door is opening or closing, update its rotation
if (isOpen || Time.time < openStartTime + openDuration)
{
float t = openCurve.Evaluate((Time.time - openStartTime) / openDuration);
Quaternion targetRotation = initialRotation * Quaternion.Euler(0f, openAngle, 0f);
transform.rotation = Quaternion.Slerp(initialRotation, targetRotation, t);
}
}
public void OpenDoor()
{
isOpen = true;
openStartTime = Time.time;
// Play the open sound
if (openSound != null)
{
audioSource.PlayOneShot(openSound);
}
}
public void CloseDoor()
{
isOpen = false;
openStartTime = Time.time;
// Play the close sound
if (closeSound != null)
{
audioSource.PlayOneShot(closeSound);
}
}
// Event, das ausgelöst wird, wenn die Tür durch den Trigger ausgelöst wurde
public UnityEvent OnDoorTriggered;
}
}
Well, I just don’t understand why the door opens automatically every time the game starts, when the condition is that the player must be in the collider and the T key must be pressed.
Can anyone give me a tip?Thanks
Here’s another video about it all.