In my project, I’m writing a function that performs a raycast from the camera’s position in the direction of the camera’s forward angle. The camera’s transform is a child of the Player, which has a CharacterController
. No CapsuleCollider
or RigidBody
. The Camera
has a local position of (0,1.75,0)
and the CharacterController
has a height of 2, and radius of 0.5
When remaining on the ground and shooting, the raycast performs as expected and ignores the Player’s CharacterController
. However, when I start jumping it will return a hit on the character controller. I tried debugging this myself and drawing a red debug line from the ray’s origin to the hit position. As we can see in the image below, the line is staying well within the confines of the CharacterController
I was thinking perhaps this is an issue with Order of Execution and maybe the physics were being updated before the scene hierarchy was updated. So I ran a debugger and put a breakpoint where it detects a CharacterController
.
Player.transform.position.y: 0.69
Camera.transform.position.y: 2.44
This is the exact same 1.75 difference I should expect.
I’m completely confused as to why this is happening. Here’s the code behind it, sans Debugging methods:
for (int i = 0; i < bulletsPerFire * _shots; i++) {
Vector3 angle = GetSpreadAngle(fwd);
int mask = 1 | (1 << 7) | (1 << 3);
RaycastHit hit;
if (Physics.Raycast(camera.transform.position, camera.transform.forward, out hit, distance, 1, QueryTriggerInteraction.Ignore)) {
IDamageable damageableObject = hit.transform.GetComponent<IDamageable>();
damageableObject?.TakeDamage(damageInfo, hit.point);
var mat = hit.GetMaterial();
if (mat != null) {
GameManager.Effects.CreateBulletDecal(mat, hit);
GameManager.Effects.CreateImpactParticleEffects(mat, hit.point, Quaternion.LookRotation(hit.normal));
}
}
}
Any ideas?