Unity可以在参考脚本中检查碰撞吗?
如您在图中所见,我们正在编码角色掉落时重新创建的逻辑。 当前,当您输入ISLEFT和ISRIGHT脚本时,位置将变为真,相反的一侧是错误的。 如果落在地板上的碰撞变为真实,我们将重生的角色真实。 管理此依据的脚本。
public class isFall : MonoBehaviour
{
public GameObject hood;
public bool isRight , isLeft;
public IsRight Position_Right;
public IsLeft Position_Left;
bool isDelay;
public Health hp;
private void Awake() {
isRight = isLeft = false;
isDelay = false;
}
private void Update() {
if(isDelay){
StartCoroutine(WaitForSpawn());
isDelay = false;
}
if (Position_Right.isRight)
{
isRight = true;
isLeft = false;
Position_Left.isLeft = false;
}
if (Position_Left.isLeft)
{
isLeft = true;
isRight = false;
Position_Right.isRight = false;
}
}
private void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.tag == "Player"){
isDelay = true;
hp.TakeDamage(1);
}
}
IEnumerator WaitForSpawn()
{
yield return new WaitForSeconds(2.0f);
if (isLeft) hood.transform.position = Position_Left.transform.position;
else hood.transform.position = Position_Right.transform.position;
}
}
// ------------------------------------------------------------------------- ----------------------------------
public class IsLeft : MonoBehaviour
{
public bool isLeft = false;
private void OnTriggerEnter2D(Collider2D collider) {
if (collider.CompareTag("Player")){
Debug.Log(" OnTriggerEnter2D = Enter");
isLeft = true;
}
}
}
iSfall可以检测到Isleft和Isigrt ontriggerenter2d吗? 该脚本正常, 但是我认为,如果我用一个脚本引用它,最好以后进行管理,所以我想知道如何进行管理。
As you can see in the figure, we are coding the logic of re-creating when the character falls off.
Currently, when you enter the isLeft and isRight scripts, the position becomes true and the opposite side is false.
If the collisions that fall on the floor become true, we respawn the characters where they are true.
The script that manages this isFall.
public class isFall : MonoBehaviour
{
public GameObject hood;
public bool isRight , isLeft;
public IsRight Position_Right;
public IsLeft Position_Left;
bool isDelay;
public Health hp;
private void Awake() {
isRight = isLeft = false;
isDelay = false;
}
private void Update() {
if(isDelay){
StartCoroutine(WaitForSpawn());
isDelay = false;
}
if (Position_Right.isRight)
{
isRight = true;
isLeft = false;
Position_Left.isLeft = false;
}
if (Position_Left.isLeft)
{
isLeft = true;
isRight = false;
Position_Right.isRight = false;
}
}
private void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.tag == "Player"){
isDelay = true;
hp.TakeDamage(1);
}
}
IEnumerator WaitForSpawn()
{
yield return new WaitForSeconds(2.0f);
if (isLeft) hood.transform.position = Position_Left.transform.position;
else hood.transform.position = Position_Right.transform.position;
}
}
//---------------------------------------------------------------------------
public class IsLeft : MonoBehaviour
{
public bool isLeft = false;
private void OnTriggerEnter2D(Collider2D collider) {
if (collider.CompareTag("Player")){
Debug.Log(" OnTriggerEnter2D = Enter");
isLeft = true;
}
}
}
Can isFall detect isLeft and isRight OnTriggerEnter2D?
The script works fine,
But I think it would be better to manage it later if I reference it with one script, so I want to know how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,您不能单独使用
isfall
仅实现这一目标,因为 ontriggerenter2d 说:在另一个对象输入附加到此对象的触发器对撞机时发送的。,您必须在左或右对撞机上附加脚本以触发回调。因此,如果您为角色创建脚本,它将能够检测所有山脉。
顺便说一句,一个字段
isleft
就足够了,因为重生位置是左或右。No you can't achieve this with
isFall
alone, because the documentation of OnTriggerEnter2D says: Sent when another object enters a trigger collider attached to this object. You must attach a script on the left or right collider to trigger the callback.So... if you create a script for the character, it is able to detect all the colliders.
BTW a single field
isLeft
is enough, because the respawn position is either left or right.