- Home /
Question by
YippyGames · Oct 04, 2020 at 07:45 PM ·
raycastphysics2draycasthit2d
My Raycast2Dhit, isnt working on prefabs
Hello!
I have been making top down shooter, and I have a script to make a tracer effect on the bullet. Within this, at the end, I want to check if the raycast hit an enemy so I can damage it. The layer of the enemy is "Enemy".There is the code: void Hit(Vector3 fromPosition, Vector3 targetPosition) { float distance = Vector3.Distance(fromPosition, targetPosition); var layerMask = LayerMask.GetMask("Enemy"); RaycastHit2D hit = Physics2D.Raycast(fromPosition, targetPosition, distance, layerMask); if (hit) { Debug.Log("Hit an " + hit.collider.name); } else { Debug.Log("Missed"); }
}
Thing is that, sometimes it works, but mostly it doesnt. (only with prefabs that are instansiated)
Comment