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?