C#_Unity_逆引き02
■下から叩くとアイテムが出るブロック
https://indie-game-creation-with-unity.hatenablog.com/entry/item-box-unity2d
https://stackoverflow.com/questions/53453014/collider-istouching-for-3d-colliders
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemBoxController : MonoBehaviour
{
public GameObject content;
private Vector3 movePoint;
private bool IsOpened;
private bool IsActive;
private BoxCollider[] m_Colliders;
private BoxCollider playerCollider;
// Start is called before the first frame update
void Start()
{
GameObject player = GameObject.FindWithTag("Player");
if (player != null)
playerCollider = player.GetComponent<BoxCollider>();
m_Colliders = GetComponentsInChildren<BoxCollider>();
content = Instantiate(content);
content.transform.position = transform.position;
content.transform.SetParent(gameObject.transform);
content.gameObject.SetActive(false);
IsOpened = false;
IsActive = true;
movePoint = (Vector3)transform.position + new Vector3(0.0f, 1.2f, 0.0f);
}
// Update is called once per frame
private void Update()
{
if (IsOpened && IsActive)
{
content.transform.position = Vector3.Lerp(content.transform.position, movePoint, 0.35f);
}
}
private void OnCollisionEnter(Collision col)
{
/*if (m_Colliders[1].IsTouching(playerCollider) && col.gameObject.CompareTag("Player"))*/
if (m_Colliders[1].gameObject.CompareTag("UnderTrigger") && col.gameObject.CompareTag("Player"))
{
content.gameObject.SetActive(true);
IsOpened = true;
StartCoroutine(WaitSwitchOff());
}
}
private IEnumerator WaitSwitchOff()
{
yield return new WaitForSeconds(1.0f);
IsActive = false;
}
}