C# if(Physics2D.CircleCast();) Not Returning True

I have a script that manipulates a line renderer to behave like lightning and I’m trying to integrate Physics2D.CircleCast(); as a means of collision detections for the line renderer. However when I place a gameobject with a collider between the lightning it never returns as true. I had the Physics2D.CircleCast(); start at source.localPosition, have it’s radius equal to tmpVfl and have it end at target.localPosition. So if(Physics2D.CircleCast():wink: should return true according to my logic. Is there something I’m missing? Apologies about my syntax as it keeps getting butchered when I paste it in my post. The syntax was fine when I was working on it.

using UnityEngine;

public class ElectroScript : MonoBehaviour {

	public LineRenderer lightning;
	public Transform source;
	public Transform destination;
	public Transform target;


	public float timeToPowerUp = 0.5f;
	

	public float keyVertexDist = 3.0f;
	public float keyVertexRange = 4.0f;
	
	public int numVertices;
	
	AudioSource aud;
	
	// Use this for initialization
	void Start () {
		numVertices = 0;
		aud = GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey(KeyCode.Equals)){
			if (aud.pitch < 3.0f) {
				aud.pitch += Time.deltaTime;
			}
			target.position = Vector3.MoveTowards(target.position, destination.position, (1.0f / timeToPowerUp));
			numVertices = Mathf.RoundToInt(Vector3.Distance(source.position, target.position) / keyVertexDist);
			Vector3 currentV = source.localPosition;
			lightning.SetVertexCount(numVertices);
			for (int i = 0; i < numVertices; i++) {
				float tmpVfl = (Random.value * 2.0f - 1.0f) * keyVertexRange;
				lightning.SetPosition(i, new Vector3(currentV.x + tmpVfl,currentV.y + tmpVfl,currentV.z + tmpVfl));
				currentV += (target.localPosition - source.localPosition) / numVertices;
				if(Physics2D.CircleCast(source.localPosition,tmpVfl,target.localPosition)){
				Debug.Log("Hit something");
				}
			}
			if(numVertices > 0){
				lightning.SetPosition(0, source.localPosition);
				lightning.SetPosition(numVertices-1, target.localPosition);
			}
		}
		if(Input.GetKeyUp(KeyCode.Equals)){
			aud.pitch = 0;
			numVertices = 0;
			lightning.SetVertexCount(0);
			target.position = source.position;
		}
	}
}

Well, first of all CircleCast works the same way as RayCast does with the difference that the “ray” has a “width”. You have to provide a direction vector, not an endpoint: Physics2D.CircleCast. Also note that you have to provide a max distance. If you don’t the cast will be infinite.

Vector3 dir = target.localPosition - source.localPosition;
if(Physics2D.CircleCast(source.localPosition,tmpVfl,dir, dir.magnitude)){
    Debug.Log("Hit something");
}

Second is your use of your “tmpVfl” seems a bit strange. You cast a ray for each point in your lightning bolt always from the start to the end. You should do only one cast after the loop with the largest “tmpVfl” that you had generated in the loop.

An alternative would be to cast a seperate ray each loop iteration, but only for the current segment. So your cast start position would be your last point and your target would be the current point. The size of the circle shouldn’t be too large.