C# SphereCast isn't Inaccurate

Hi everyone, I have been a using a SphereCast instead of a Raycast to try and make it more inaccurate. However my SphereCast doesn’t seem that inaccurate and still misses the object I want to hit. Any idea what is wrong with my script?

public float Distance = 100;

public RaycastHit hit;

if(Physics.SphereCast(gameObject.transform.position, 0.5f, transform.forward, out hit, Distance))

The only problem might be the physics not being updated when you call the sphere cast if the object is moving fast. You should try doing a Debug.DrawRay to ensure that you are pointing where you think you are.

–

Just as long as you understand the difference between Vector3.forward and transform.forward. Vector3.forward is in world space, meaning that it always points along the world's z axis, while transform.forward is in local space, meaning that it points along the transform's z axis. Is your transform rotated 90 degrees along the x axis? This would cause transform.forward to point down.

–

1 Answer

1

I figured it out. I switched transform.position to gameobject.transform.position since I’m using this with Unity’s default FPS character controller I want it to be relevant to that gameobject.

That effectively does nothing, because gameObject.transform and transform are the same thing. ;)

–

You have to change the "Clear flags" setting in the camera to "don't clear". After a recent update to Unity 2017.3.0 I had this problem in my app this solution worked for me! Thanks!

–