Raycasting works properly in Debug Mode but NOT in Release Mode


I was having trouble with my InteractWithCombat() Method, specifically on the Raycast detection.

I initially tried multiple fixes to no avail:

  1. Ensuring the right scripts are attached to the enemy, the code is proper and the box collider is accurately placed on the enemy mesh. Ensured that Debug.DrawRay was on and that I was actually hitting the box collider.
  2. Turning off Queries Start In Colliders in Edit>ProjectSettings>Physics2D .
  3. Screen size was consistent and set at the same size for both Debug and Release Mode (16:9).

Here’s where it gets interesting. After hours of tinkering and debug statements everywhere, I found out that there are two things make the detection work again:

  1. When I am in Release Mode, I would have to fire UNDER the enemy box collider to trigger the InteractWithCombat() Method, whereas in Debug Mode I could just click at the box collider and everything works out nice.
  2. When I check off “Use Gravity” in my enemy’s rigidbody component (see below), suddenly the detection works well even in Release Mode. Please note that I DO have a box collider on my ground.

The only things that are different from the lectures is that I am using CinemachineVirtualCamera with a Cinemachine Collider Script instead of the camera used in the lectures, and that my enemy has a rigidbody. I can honestly move forward by just removing the rigidbody on the enemy, but I was interested in potential reasons on why the code wasn’t working as intended, and to see if anyone had any insight on this matter. Thank you.

Sorry about the delay on this

First, make sure that all the colliders are the non-2D variety and that you’re working on settings in Edit/ProjectSettings/Physics. Unless you’re working with 2D, in which case you should have everything being 2D, using RigidBody2D, etc. If you’re following along with the course, however, you’ll have the easiest time the first time through making the game in 3d.

Since we’re using NavmeshAgents, our Rigidbodies should generally be set to IsKinematic and have Use Gravity unchecked. The NavMeshAgent should be handling keeping the character on the ground.
You can get unpredictable results letting the rigidbody manage gravity as the rigidbody and the navmeshagent battle one another.

Privacy & Terms