Open the door with a space key

In Unity, I want to make a door slide open and close when the player enters a certain area and presses the space key. Only the Box Collider attached to a child object under the Door object will move. This child object is an empty object created for player intrusion detection. The result is the same whether I create an animation or control it only from a script. By the way, the attached script is for the case where it is controlled only from the script. There are no errors, and debugging shows that it works well. Below are two scripts and the Unity edit screen.

<“DoorOpenScript”>

using UnityEngine;

public class DoorOpenScript : MonoBehaviour {
    public float slideDistance = 2.0f; // ドアがスライドする距離
    public float slideSpeed = 2.0f;    // ドアのスライド速度
    private bool isNear = false;       // プレイヤーが近くにいるかどうか
    private bool isOpen = false;       // ドアが開いているかどうか
    private Vector3 closedPosition;    // ドアの初期位置
    private Vector3 openPosition;      // ドアの開いた位置

    void Start() {
        closedPosition = transform.position;
        Debug.Log("オブジェクトの位置: " + closedPosition);
        openPosition = closedPosition + transform.forward * slideDistance; // スライド方向を調整(右方向)
    }

    void Update() {
        if (isNear && Input.GetKeyDown(KeyCode.Space)) {
            isOpen = !isOpen; // スペースキーで開閉を切り替え
            Debug.Log("ドアの状態が切り替わりました: " + (isOpen ? "Open" : "Closed"));
        }

        // ドアを開く・閉じる位置に線形補間で移動
        transform.position = Vector3.Lerp(transform.position, isOpen ? openPosition : closedPosition, Time.deltaTime * slideSpeed);
        Debug.Log("ドアの現在位置: " + transform.position);
    }

    public void PlayerEnteredArea() {
        isNear = true; // プレイヤーが近くにいると設定
    }
 
    public void PlayerExitedArea() {
        isNear = false; // プレイヤーが近くにいないと設定
        Debug.Log("プレイヤーがトリガーエリアから出ました。ドアは閉じます。");
    }
}

<“Search Area Trigger” Script>

using UnityEngine;

public class SearchAreaTrigger : MonoBehaviour {
    // ドアオブジェクトの参照
    private DoorOpenScript doorOpenScript;

    void Start() {
        // 親オブジェクトからDoorOpenスクリプトを取得
        doorOpenScript = transform.parent.GetComponent<DoorOpenScript>();
        if (doorOpenScript == null) {
            Debug.LogError("DoorOpenスクリプトが親オブジェクトに見つかりません!");
        }
    }

    void OnTriggerEnter(Collider col) {
        if (col.CompareTag("Player")) {
            // プレイヤーがトリガーエリアに入ったとき
            doorOpenScript.PlayerEnteredArea(); // ドアのスクリプトにプレイヤーが入ったことを通知
            Debug.Log("プレイヤーがトリガーエリアに入りました。");
        }
    }

    void OnTriggerExit(Collider col) {
        if (col.CompareTag("Player")) {
            // プレイヤーがトリガーエリアから出たとき
            doorOpenScript.PlayerExitedArea(); // ドアのスクリプトにプレイヤーが出たことを通知
            Debug.Log("プレイヤーがトリガーエリアから出ました。");
        }
    }
}

All this sounds great. I assume something isn’t working?

In that case perhaps you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.


If you’re doing animation…

Anything with Animations / Animators / Mechanim:

Only consider the code AFTER you have done this critical step:

Always start with the Animator state machine and prove it works in isolation, no code at all.

Here’s more reading:


This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?