SphereCast issues

So I have 1 separate projectile controllers, one of them is a basic LineCast method (for smaller projectiles), and the second I’m trying to get working with a SphereCast method (for large projectiles). But I’m having trouble getting it to work properly.

Here is my LineCast update code:

transform.Translate( new Vector3 (0.0f, 0.0f, (travelSpeed * Time.deltaTime)) );
		
		RaycastHit hit;
		if(Physics.Linecast(lastPosition, transform.position, out hit))
		{
			Debug.Log("Object has hit: " + hit.transform.name + "!");
			Destroy(gameObject);
		} 
		lastPosition = transform.position;

Trying to convert that in to a SphereCast, here is my SphereCast code:

transform.Translate( new Vector3 (0.0f, 0.0f, (travelSpeed * Time.deltaTime)) );
		
		RaycastHit hit;
		if( Physics.SphereCast(lastPosition, 1f, Vector3.forward, out hit, (travelSpeed*Time.deltaTime) ) )
		{
			Debug.Log("Object has hit: " + hit.transform.name + "!");
			Destroy(gameObject);
		}
		lastPosition = transform.position;

Now the sphere is working perfectly sometimes, and other times it just goes right thru characters. And I’m not sure why.

I’m assuming it’s probably in the SphereCast line itself, because the sphere radius of 1 should be larger than the actual projectile I’m using.

If I want the SphereCast to work just like the LineCast code above, but only with a wider radius, are there any glaring mistakes?

Vector3.forward should be the same direction as translating on the Z, correct?

Do I need to do transform.TransformDirection somewhere in there?

UPDATE- I’ve added a WebPlayer demo to show the issue I’m having: Click Here.

Run at the spheres that are being launched and you will see that the collision is inconsistent. It works more often than not but at times goes right thru the player.

Any advice?

Doing a little more testing, it seems that there’s only collision issues at lower speeds, higher speeds work properly, but sometimes lower speeds just go right thru the target. (you can see this at the lower speed in the webplayer linked above).

It also seems that, even though I’m using lastPosition as the origin, and travelSpeed as the distance… the collision is in FRONT of the projectile, rather than on or behind it. If I shoot it at a speed slower than the characters run speed, I have to run directly thru it and then in front of it before it registers the hit correctly.

So there must be a problem in the SphereCast somewhere (most likely Origin/Distance) but I’m having trouble getting it working properly…

Anyone?

Its a little hard to tell, but after playing your web player build my guess is that you’re somehow not computing the distance of the SphereCast correctly, so every now and then the player happens to be in its ‘blind spot’.

(travelSpeed*Time.deltaTime) should be right, but I feel uneasy about it because deltaTime gets very small as the framerate increases. You might want to try substituting : Vector3.Distance(transform.position, lastPosition) to see if that’s any more reliable, or move the SphereCast into FixedUpdate() so you’re dealing with more predictable intervals. You can still move the projectile in Update() though.

Good luck!

Thanks for the response!

I’ll try your suggestions and see how it goes.

So I gave it a try, I both moved the SphereCast to FixedUpdate and changed the lines you suggested, still having the same exact issue. I actually think it may have gotten worse, as soon as starting the first 5 projectiles went thru me a few times in a row. It went thru when not moving quite a few times as well.

Very strange, not sure why it’s being so inconsistent…

Not sure what to do…

(Edit: So it seems that SphereCast does not actually cast the sphere from the origin, or something along those lines.

If I cast the sphere from lastPosition forward, the collision box is aligned with the very front of the projectile, but if I run thru the projectile from behind, it will not collide until I’m in front of it. Which means it’s only registering once I am in front of it.

If I cast the sphere from transform.position, directed backwards, the collision does not happen until the projectile fully passes thru the target.

So it seems the entire sphere is not actually colliding? The description of spherecast says it casts a sphere at origin, sweeps it to the target, and returns any collisions… but in reality it seems that it is not capturing any collisions at the origin at all?

Or it could be that the positioning of the sphere is not centered? Because in my projectile, the origin is at the center of the projectile… if the SphereCast origin was centered then if you use transform.position as the origin, then it should be picking up any collisions for the entire projectile. But it’s not picking up the entire projectile - only the edge of the direction it is sweeping?

Pretty much seems that the entire sphere at origin, and the entire sphere at the destination, will not pick up any collisions… ONLY what is picked up between the distance of the origin and destination.

Only problem with that it’s troublesome to get the origin lined up correctly…

Your direction is bad. you want to use it like this:
Physics.SphereCast(lastPosition, 1f, ( transform.position - lastPosition ), out hit, (travelSpeed*Time.deltaTime) )

Sphere cast is more like a cylinder cast in that it doesnt check caps. So what your doing should be fine,its just you were always casting forward thats why.

Also you’ll want to make sure transform.position != lastPosition. Sending a zero mag vector might cause errors.