I think you’re right! That’s wild… I confirmed it easily even back in ancient Unity3D 5.6.6f2 with my own setup and your numerals. WOW… it totally misses until you wiggle something a bit.
I am now going to go into pure 100% random wild guess to explain this bug: It is some weird interaction between the two spheres at each end and the cylinder check… like it is determined to hit one from the inside and then the other isn’t checked. That’s my SWAG.
I’d love it if someone from Unity can offer insight here…
My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RayToCapsule : MonoBehaviour
{
[Header( "You can drag these in and modify them in editor,")]
[Header( "... or see what they are initted to in Start();")]
public Transform From;
public Transform To;
void Start()
{
// numbers from original post:
//
// https://discussions.unity.com/t/recreatable-incorrect-raycast-miss-on-capsule-collider/1556381
//
// Verified that this From / To combination does NOT hit the capsule in Unity5!!
From.position = new Vector3(-0.4902608f, 1.836351f, 0.4888007f);
Vector3 direction = new Vector3(0.3221217f, -0.8907961f, -0.3204995f);
float length = 2.0f;
To.position = From.position + direction.normalized * length;
}
void Update ()
{
Ray ray = new Ray(origin: From.position, direction: To.position - From.position);
RaycastHit rch;
bool hit = false;
if (Physics.Raycast( ray, out rch, maxDistance: 2))
{
hit = true;
}
Color color = hit ? Color.green : Color.white;
Debug.DrawLine(From.position, To.position, color);
}
}
Enclosed is the package with micro-scene setup.
RayToCapsule.unitypackage (3.9 KB)