我正在开发Unity项目,它是经典的Mario风格2D Platformer。发生的问题是与一个已知的敌人[[Snail相关,当被击晕时可以将其踢开。
[有两个功能,两个都可以单独使用,但不能一起使用。项目Build已导出为WebGL,可以在浏览器中播放。问题描述
:当我在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。发生的问题是与一个已知的蜗牛有关,它可以在昏迷时被踢开。有两个...
WebGL