我如何检测一个角色是否已经在另一个游戏对象中?

How can i detect if a character is already inside another gameobject?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour {

    enum MoveProperties
    {
        DirectionStart,
        DirectionEnd
    };

    public float spinSpeed = 2.0f;

    private bool rotate = false;
    private bool exited = false;
    private List<GameObject> prefabs;

    private void Start()
    {
        InstantiateObjects gos = GetComponent<InstantiateObjects>();

        prefabs = new List<GameObject>();
        prefabs = gos.PrefabsList();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            Debug.Log("Player entered the hole");
            rotate = true;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            Debug.Log("Player exited the hole");
            rotate = false;
            exited = true;
        }
    }

    void Rotate()
    {
        if (rotate)
        {
            transform.Rotate(Vector3.up, spinSpeed * Time.deltaTime);
            spinSpeed += 1f;
        }
        if (rotate == false && exited == true)
        {
            transform.Rotate(Vector3.up, spinSpeed * Time.deltaTime);
            if (spinSpeed > 0.0f)
                spinSpeed -= 1f;
        }
    }

    private void Update()
    {
            Rotate();
    }
}

这里有两个触发器:

OnTriggerEnter 和 OnTriggerExit。

但是如果角色先出去然后又进去,这个触发器就会起作用。或者出去了。但是我需要检测角色是否已经在里面。 不用先出去再进来。

情况是角色首先在一个地方(对象内部的洞),然后我将角色位置更改为另一个有洞的对象。改变位置后,我需要以某种方式检测角色是否在第二个洞内。

But this triggers will work if the character first went out and then went in again. Or went out. But i need to detect while the character is already inside. Without first going out and getting in again.

您正在查找 OnTriggerStay 函数。只要两个 Collider 都在彼此内部,OnTriggerStay 将始终在每一帧调用。

void OnTriggerStay(Collider other)
{

}

如果您决定使用碰撞而不是触发器,还有 OnCollisionStay

void OnCollisionStay(Collision collisionInfo) 
{

}