Dan*_*oni 0 c# unity-game-engine unity5
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这里有两个触发器:
OnTriggerEnter和OnTriggerExit.
但是如果角色先出去然后再进去,这个触发器就会起作用.或者出去了.但我需要检测角色已经在里面.没有先出去再次进入.
情况是角色首先在一个地方(对象内的洞),然后我将角色位置更改为另一个有洞的对象.在改变位置之后我需要以某种方式检测到角色在第二洞内.
但是如果角色先出去然后再进去,这个触发器就会起作用.或者出去了.但我需要检测角色已经在里面.没有先出去再次进入.
您正在寻找这个OnTriggerStay功能.只要两个碰撞器都在彼此内部,OnTriggerStay就会始终每个帧都被调用.
void OnTriggerStay(Collider other)
{
}
Run Code Online (Sandbox Code Playgroud)
OnCollisionStay如果您决定使用碰撞而不是触发器,也可以使用.
void OnCollisionStay(Collision collisionInfo)
{
}
Run Code Online (Sandbox Code Playgroud)