raycast2D not working.
I'm making a AI to follow the player, but if the AI is near a pit, it will stop. i'm using raycast2D, and a if statement:
RaycastHit2D hitLeft = Physics2D.Raycast(leftEdge.transform.position, Vector2.down, 0.6f); RaycastHit2D hitRight = Physics2D.Raycast(rightEdge.transform.position, Vector2.down, 0.6f); if (hitLeft.collider == null) { speed = 0f; Debug.Log("Left's not working"); } if (hitRight.collider == null) { speed = 0f; Debug.Log("Right's not woking"); } **
but it doesn't matter if the collider is null or not, it will always change the speed to 0 and do both debug.logs.
(sorry for my bad English btw)
Answer by hukado · Feb 08, 2018 at 10:44 PM
never mind. already fixed it
RaycastHit2D hitLeft = Physics2D.Raycast(leftEdge.transform.position, Vector2.down); RaycastHit2D hitRight = Physics2D.Raycast(rightEdge.transform.position, Vector2.down); if (hitLeft.collider != false) { mainBody.position = Vector2.MoveTowards(mainBody.position, new Vector2(target.position.x, transform.position.y), speed * Time.deltaTime); } else { mainBody.position = Vector2.MoveTowards(mainBody.position, new Vector2(-target.position.x, transform.position.y), speed * Time.deltaTime); } if (hitRight.collider != false) { mainBody.position = Vector2.MoveTowards(mainBody.position, new Vector2(target.position.x, transform.position.y), speed * Time.deltaTime); } else { mainBody.position = Vector2.MoveTowards(mainBody.position, new Vector2(-target.position.x, transform.position.y), speed * Time.deltaTime); }
.
Your answer
Follow this Question
Related Questions
Please Help I have a problem with my game animations 0 Answers
Why the Unity is not opening? 0 Answers
UI broken after closed without saving on accident! 0 Answers
Cant setactive(); 0 Answers