I’m experiencing an issue with 3D raycasting in a 2D setting. The player can create a projectile relative to their position, which destroys itself if it hits an enemy and passes damage information to the enemy it hits. The player and enemies cannot collide (enemies, however, cannot overlap each other). The projectile uses raycasting to check for collisions along the x-axis. So the issue is that when the projectile is spawned too close to an enemy, or even inside an enemy, the raycasting will not hit the object and just pass through it.
My attempted solution was to change the axis where collisions would be detected to the z-axis. That, however, requires an offset to point where the raycast begins.
Here is a snippet of the code where I do the raycasting (part of a function called from update):
if (Physics.Raycast(transform.position + new Vector3(0, 0, 5), -Vector3.forward, out hit, Mathf.Infinity, -1))
{
if (hit.transform.tag == "AI")
{
print("Collision Detected On Z-Axis");
hit.transform.GetComponent<AIBehavior>().SendMessage("takeDamage", dmgMod);
Destroy(this.gameObject);
}
}
Should I check the x-axis with this, the collision detection never finds anything which makes me think that adding the vector3 to it is doing something, but I’m still at lost on how to make this work.
Any thoughts?