RaycastHit2D在另一个协同程序处于活动状态时停止工作。 Unity2D Platformer,W ebGL

问题描述 投票:-1回答:1

我正在开发Unity项目,它是经典的Mario风格2D Platformer。发生的问题是与一个已知的敌人[[Snail相关,当被击晕时可以将其踢开。

[有两个功能,两个都可以单独使用,但不能一起使用。项目Build已导出为WebGL,可以在浏览器中播放。

1。 ChangeDirection:

ChangeDirection()是一个协程,在开始功能中被调用。当到达平台末端时,它会翻转蜗牛的精灵和运动方向。

2。 CheckCollision

在更新功能中调用CheckCollision并检测附着在蜗牛上的4个游戏对象上的碰撞。

问题描述

当我在Start-Function内注释

“ StartCoroutine(ChangeDirection());”

时,CheckCollision正常工作。蜗牛被打晕后,我可以把它打晕并踢开。当我将ChangeDirection()-Coroutine放回原位时,蜗牛会完美地改变其方向。还可以检测到top_Collision。我可以使蜗牛眩晕,它会更改动画并停止移动,但是左右碰撞似乎停止工作,因此我

无法将蜗牛踢开。假设

ChangeDirection()协程不会完全阻止CheckCollision(),因为检测到顶部碰撞仍然有效。 topHit是Collider2D,而leftHit和rightHit是RaycastHit2D。由于某种原因,RaycastHit2D无法与同时激活的ChangeDirection()协程一起使用。

using System.Collections; using System.Collections.Generic; using UnityEngine; public class SnailScript : MonoBehaviour { public float moveSpeed = 1f; private Rigidbody2D myBody; private Animator anim; public LayerMask playerLayer; private bool moveLeft; private bool canMove; private bool stunned; public Transform left_Collision, right_Collision, top_Collision, down_Collision; private Vector3 left_Collision_Pos, right_Collision_Pos; void Awake() { myBody = GetComponent<Rigidbody2D>(); anim = GetComponent<Animator>(); left_Collision_Pos = left_Collision.position; right_Collision_Pos = right_Collision.position; } void Start() { moveLeft = true; canMove = true; StartCoroutine(ChangeDirection()); } void Update() { if (canMove) { if (moveLeft) { myBody.velocity = new Vector2(-moveSpeed, myBody.velocity.y); } else { myBody.velocity = new Vector2(moveSpeed, myBody.velocity.y); } } CheckCollision(); } void CheckCollision() { //ATACHED GAMEOBJECTS FOR COLLISION DETECTION RaycastHit2D leftHit = Physics2D.Raycast(left_Collision.position, Vector2.left, 0.1f, playerLayer); RaycastHit2D rightHit = Physics2D.Raycast(right_Collision.position, Vector2.right, 0.1f, playerLayer); Collider2D topHit = Physics2D.OverlapCircle(top_Collision.position, 0.2f, playerLayer); if (topHit != null) { if (topHit.gameObject.CompareTag(MyTags.PLAYER_TAG)) { if (!stunned) { //Player does a little rebounce after jumping on the snail topHit.gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(topHit.gameObject.GetComponent<Rigidbody2D>().velocity.x, 7f); canMove = false; myBody.velocity = new Vector2(0, 0); anim.Play("Stunned"); stunned = true; } } } if (leftHit) { if (leftHit.collider.gameObject.CompareTag(MyTags.PLAYER_TAG)) { if (stunned) { //SNAIL KICKED TO THE RIGHT myBody.velocity = new Vector2(15f, myBody.velocity.y); } } } if (rightHit) { if (rightHit.collider.gameObject.CompareTag(MyTags.PLAYER_TAG)) { if (stunned) { //SNAIL KICKED TO THE LEFT myBody.velocity = new Vector2(-15f, myBody.velocity.y); } } } } IEnumerator ChangeDirection() { if (!Physics2D.Raycast(down_Collision.position, Vector2.down, 0.1f)) { moveLeft = !moveLeft; Vector3 tempScale = transform.localScale; if (moveLeft) { //SNAIL LOOKS TO THE LEFT tempScale.x = Mathf.Abs(tempScale.x); //Changing position of the attached GameObjects for collision detection left_Collision.position = left_Collision_Pos; right_Collision.position = right_Collision_Pos; } else { tempScale.x = -Mathf.Abs(tempScale.x); left_Collision.position = right_Collision_Pos; right_Collision.position = left_Collision_Pos; } //new sprite-orientation transform.localScale = tempScale; } yield return new WaitForSeconds(0.5f); StartCoroutine(ChangeDirection()); } }

我正在开发Unity项目,它是经典的Mario风格2D Platformer。发生的问题是与一个已知的蜗牛有关,它可以在昏迷时被踢开。有两个...
unity3d unity-webgl
1个回答
0
投票
我终于发现,当将构建导出为

WebGL

© www.soinside.com 2019 - 2024. All rights reserved.