Physics.SphereCast returning incorrect hit normal?

Part of my code currently uses raycasting, and I’ve just discovered the new spherecast which would work a lot better in my code due to the fact that as it states “Think of the sphere cast like a thick raycast.”.

So I’ve changed my code to use the new spherecast instead, but it’s returning the hit normal incorrectly (or at least differently to what it does with a regular raycast/linecast). Does this have something to do with it being spherical rather than a single line?
The normal it returns seems to be close to correct, but slightly off, and seems to be somewhat related to the angle that the spherecast hits the ground at, instead of returning the world space normal of the surface it hit.
It’s on flat ground, so it should be returning a normal of (0,1,0), but instead I get values ranging from about (0.2,1,0) to (-0.2,1,0). I have confirmed through both code and visually that the raycast is still hitting the correct spot. I’ve tried changing the values for the radius from tiny (0.01) up to much bigger values (0.2, the entire thickness of the character)

Both linecast and raycast have always given me the ground normal as I expected, but spherecast is returning a value that varies. Since it returns a standard RaycastHit, I have assumed it functions the same, and the help file doesn’t indicate otherwise.
Has anyone else dealt with spherecast and found similar problems?

Haven’t used it, but if you have a perfectly flat plane and the RaycastHit isn’t returning the expected normal for a flat plane then you should probably file a bug report. Try it in a clean project and project against a simple plane and a clean (flat) terrain to be sure that it’s a bug.

Being a Unity noob, I’m always reluctant to jump to the conclusion of bug before asking the forum first. I just wasn’t sure if this was intended behaviour for the spherecast somehow. I tried it with a blank scene and still had the same problems. For anyone who wants to see my problem and see if I’m wrong, try this-

Create a plane (scaled to 2,2,2), and attach this script to it-

function Start() {
	var hitinfo : RaycastHit;

	var hit = Physics.SphereCast(Vector3(0,1,0), 0.1, Vector3(1,-1,0), hitinfo, 2);


	if (hit)
	{
		Debug.Log(hitinfo.normal.x);
	}

}

If you print the RaycastHit.normal, it gives you (0,1,0) like you’d expect, but when you print out the individual x component for accuracy, you see that it returns a small value (I get -0.0475) instead of 0. It may not sound like much, but it’s wrong, and it’s making it unusable for my game. The value it returns varies depending on the parameters you use, which shouldn’t be the case as far as I’m aware. Can anyone else confirm that this is a bug?

and what happens if you use it reasonably ie not attached to the object you want to cast against? (cause if it worked reasonably it wouldn’t return you a hit at all if you cast from the plane itself as it intersects from moment 0 on - this might also be the reason for the error, a trivial IEEE error due to the first hit taking part ~0)

I assume in your real case you use layers to exclude “your own mesh” from being hit at all

My own mesh doesn’t even have a collider (all done with raycasting), so no chance of that, but I am using layers to only hit the level geometry. The only reason I’ve attached it to the ground object here is for simplicity. You can go and attach it to an empty GameObject instead and it doesn’t change the result. I only placed it in Start here so it would only return a Debug message once as opposed to continuously. You can stick that in the regular Update and it also doesn’t change the result either. In my own game where I first saw the problem the script isn’t attached to the object being cast against, and it’s in Update.

This works perfectly for me:

Physics.SphereCast(new Ray(this.transform.position + new Vector3(0,this.transform.localScale.y,0), -this.transform.up), this.transform.localScale.y * 0.5f, this.transform.localScale.y))