- Home /
Physics 2D Raycast is not working
I have a basic enemy that is supposed to move towards the player when the raycaster collides with the player. I have been at it for a couple of days and cannot figure out what I am doing wrong. Here is the code I have written.
void Update () { RaycastHit2D hit = Physics2D.Raycast (transform.position, transform.localScale.x * Vector2.right, sight); if(hit.collider != null && hit.collider.tag == "character"){ GetComponent ().AddForce(Vector3.up*force + (hit.collider.transform.position-transform.position)*force); Debug.Log ("Player nearby");
}
}
The debug log is not doing anything either and no matter what I change the tag to or if I even take the tag off it is not doing anything. Any help at all would be greatly appreciated!
First thing I ask any raycasting question: Does your player have a collider on it and active?
Answer by cjjacobs97 · Oct 05, 2017 at 04:12 AM
Yes, I have a collider on it and it is active. My player walks around and collides with the ground and other triggers, but the raycast coming off of this enemy is not detecting the player. I tried setting the tag to a different object and it detected that, but no matter what I have tried it will not detect my character.
Answer by Tu-Le · Oct 05, 2017 at 04:46 AM
there are very many reasons that can make raycast not work as you wish, you should:
1st Debug when Hit.collider not null the name of hit.collider.gameobject
2nd, Check the position of Enemy on Editor, maybe it's lower than Player Collider
3rd, you use vector2.right as direction, you can try ignore local Scale, only vector2.right and watch if its work? (im afraid something local scale x not = 1).
4th, you should use layerMask for only find Player, the raycast only find 1 Hit, the nearest hit, that mean if the nearest hit not have Tag == Player, youre fail
in case you wanna find many Hit, use raycastall
I debugged and nothing happens. If I change the tag to a ceratain other object it does debug though. I am using OnDrawGizmos so I can see my raycast and it is indeed touching the player. I got rid of the local scale and nothing happened still. Here is my code now witht he layer mask.
void Update () {
RaycastHit2D hit = Physics2D.Raycast (transform.position, Vector2.right, sight, Layer$$anonymous$$ask.NameToLayer ("player"));
if(hit.collider != null && hit.collider.tag == "character")
{
Debug.Log ("Player Detected");
GetComponent<Rigidbody2D>().AddForce(Vector3.up*force + (hit.collider.transform.position - transform.position)*force);
}
} Still nothing, but If I check to see if the object is equal to null I get a null reference exception as soon as the game starts up.
your code easy work for me :) check your setup project
image : https://imgur.com/a/Hws9O
Ok, so I found out that if I start the game witht he player in the same position as the enemy it detects it, but once it moves out of the way it doesnt detect it again and if I start away from the enemy and walk to where it is standing it still does not detect it. I have tried loading into another scene and deleting everything except the character and enemy with no luck still
Ok so I have found out why it is not working. I have colliders in each room of my game that act as camera bounds. If I delete the camera bounds or move them away from the enemy the raycast works. The colliders are not even on the same layer, but regardless of whether or not I select trigger or collider it stops the raycast from detecting the player. Here is the code I tried, but this stops detecting the player even if the collider is not there. void Update{
RaycastHit2D hit = Physics2D.Raycast (transform.position, transform.localScale.x * Vector2.right, sight, Layer$$anonymous$$ask.NameToLayer ("player"));
if(hit.collider!= null && hit.collider.tag == "character"){
Debug.Log("Player Detected");
GetComponent<Rigidbody2D>().AddForce(Vector3.up*force + (hit.collider.transform.position - transform.position)*force);
}
Answer by Content1of_akind · Apr 21, 2019 at 09:16 PM
If you click the output icon, then your print, debug.log etc... Should show on the Console Output Window. Window > General > Console. I hope this would help cause this solved my issue with raycast not displaying on Console Window.
Your answer
![](https://koobas.hobune.stream/wayback/20220612140302im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change Transparency 1 Answer
Need help with OnUse() script! 1 Answer
Want to move object slowly to where the mouse clicks? 1 Answer
Break Block Like Minectaft 2 Answers
Unity android screen resolution 1 Answer