Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

扉の開閉 #41

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Assets/SSP/Scripts/Common/DoorController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorController : MonoBehaviour, IInteractable //IInteractableを継承
{
bool isOpen; //falseが閉まっている状態、trueが開いている状態
public Transform Stick;
float timer;
//Interactメソッドが呼ばれるとisOpen切り替え、trueかfalseでOpenかCloseのメソッド呼び出し
public void Interact(PlayerManager playerManager)
{
isOpen = !isOpen;
if (isOpen)
{
StartCoroutine("Open");
}
else if (!isOpen)
{
StartCoroutine("Close");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ドアの開閉命令はコルーチンではなく,メソッド(Open,Close)を作ってその中でやってください.
(isOpenの値が変化したらドアの開閉を行うメソッドを呼ぶ)
コルーチンを使う場合はOpen,Closeの中で開始するようにしてください

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

14行目からここまでの処理はUpdateの中でやってください.( isOpenの値の監視は今回は一時的にUpdate内で行う)
基本的にInteract文の中ではisOpenの値だけを切り替えるようにしてください

}
void Start()
{
isOpen = false; //最初は閉まっている
timer = 0;
var player = GameObject.Find("Player");
Stick = player.transform;
}

void Update()
{
Debug.Log(isOpen);
//Debug.Log(Stick.position);
}

private IEnumerator Close()
{
for (int i = 0; ; i++)
{
timer += Time.deltaTime;
transform.position += new Vector3(10.0f, 0.0f, 0.0f);
Debug.Log(transform.position);

if (timer >= 0.5f)//2秒間実行?
{
timer = 0;
yield break;
}
yield return null;
}

}

private IEnumerator Open()
{
for(int i=0; ; i++)
{
timer += Time.deltaTime;
transform.position += new Vector3(-10.0f, 10.0f, 0.0f);
Debug.Log(transform.position);

if (timer >= 0.5f)
{
timer = 0;
yield break;
}
yield return null;
}
}
}