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() 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;
}
}
}