- Home /
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();) 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;
}
}
}
Answer by Bunny83 · Jun 06, 2016 at 08:52 PM
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.
I'm afraid I don't quite understand. I can't get the largest tmpVfl value because tmpVfl is constantly being rewritten. I also tried declaring tmpVfl as a variable but if(Physics2D.CircleCast()) is still returning false .
for (int i = 0; i < numVertices; i++) {
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;
}
Vector3 dir = target.localPosition - source.localPosition;
if(Physics2D.CircleCast(source.localPosition,tmpVfl,dir, dir.magnitude)){
Debug.Log("Hit something");
Debug.DrawRay(source.localPosition, dir, Color.red);
}
Wasn't I already casting a separate ray each loop iteration? the if(Physics2D.CircleCast()) was inside the for loop so it's being declared over and over. Is that why if(Physics2D.CircleCast()) isn't returning true?
Also is debug.drawray a good way of visualizing the Physics2D.CircleCast(); or is there something more dynamic I could use ins$$anonymous$$d? I'm getting no output so I can't tell whats going on(or not going on as is the case).
Your answer
Follow this Question
Related Questions
RTS Camera movement wrong after rotation 1 Answer
Position in Vector (C#) 1 Answer
position.Set vs. position = new Vector3() 0 Answers
Distribute terrain in zones 3 Answers