问题
I am doing a mario prototype and (in this context) i am using two Raycasts, one pointing from mario up to the top of his head. That is used when mario is touching the blocks in the air. The second raycast is used down to the bottom of the feet to detect when he is stepping on a Goomba (enemy). I have included a function down below which shows that i use the same structure on both of the rays and "if" statements, but despite that, only the first one (on head) works.
public void marioRaycast()
{
RaycastHit2D hitUp = Physics2D.Raycast(transform.position, new Vector2(0, 1), hitUpDistance); // Raycast som pekar upp, +1
RaycastHit2D hitDown = Physics2D.Raycast(transform.position, new Vector2(0, -1), hitDownDistance); // Raycast som pekar ner, -1
Debug.DrawRay(transform.position, new Vector2(0, hitUpDistance), Color.green); // Visualiserar en "raycaststråle" för att debugga.
Debug.DrawRay(transform.position, new Vector2(0, hitDownDistance), Color.green); // Visualiserar en "raycaststråle" för att debugga.
if (hitUp && hitUp.collider.tag == "CoinBox")
{
questionObject = hitUp.collider.gameObject;
if (questionObject.GetComponent<QuestionmarkBox>().coinBoxStatus)
{
if (questionObject.GetComponent<QuestionmarkBox>().getRandomNr() <= 4) // "40%" för poäng ifrån box
{
ScoreHandler.AddScore(100);
}
else // Låg chans för powerup.
{
questionObject.GetComponent<QuestionmarkBox>().instantiatePowerUp();
}
}
questionObject.GetComponent<QuestionmarkBox>().coinBoxStatus = false; // Stänger av blockets relevans efter kollision
}
if (hitDown && hitDown.collider.tag == "enemyGoomba")
{
Debug.Log("You are now in the goomba if");
GetComponent<Rigidbody2D>().AddForce(Vector2.up * 100);
GetComponent<Rigidbody2D>().AddForce(Vector2.right * 200);
Destroy(hitDown.collider.gameObject); // Förstör goomban.
}
}
If you need more information on the script tell me. And in the last "if" statement it has not reached the "Debug.Log" yet, which is my goal. It doesn't seem listen to either the last statement or maybe the ray / possibly enemy?
I dont see how the 2nd ray is not working just because of the earlier ray working when theres no difference to them both, except some "values". What could it be?
来源:https://stackoverflow.com/questions/47582895/raycast2d-not-working-as-intended