Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ochreous · Jun 06, 2016 at 08:32 PM · c#colliderpositionvector3physics2d

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





Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ochreous · Jun 07, 2016 at 03:10 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

163 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Script to make Camera follow the player C# 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges