Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Ochreous · Jun 10, 2015 at 06:21 PM · c#colliderrendererlineedge

C# Calculate Edge Collider for Line Renderer

I need to create an edge collider for a line renderer with the following script below attached. So I was able to calculate the line renderer's positions and vertices but I'm not sure how to implement an edge collider. I assume I want to set the edge collider's points to equal the line renderer's number of positions? Also I wasn't able to find edgeCollider2d.setPoint. Is this done differently to setPosition?

 using UnityEngine;
 using System.Collections;
 using Generic = System.Collections.Generic;
  
 public class BeamScript : MonoBehaviour {
     public Transform startPos;
     public Transform beam;
     public float destFloat = 1f;
     public float timeToUpdate = 0.05f;
     public float timeToPowerUp = 0.5f;
     public float keyVertexDist = 3.0f;
     int numVertices;
     float lastUpdate;
  
     // Use this for initialization
     void Start () {
         numVertices = 0;
         lastUpdate = 0.0f;
     }
  
     // Update is called once per frame
     void Update () {
         Vector3 movePositions = new Vector3(beam.transform.position.x + Input.GetAxis("Horizontal"),beam.transform.position.y + Input.GetAxis("Vertical"),beam.transform.position.z);
         beam.transform.position = Vector3.Lerp(beam.transform.position, movePositions, Time.deltaTime);
         if ((Time.time - lastUpdate) < timeToUpdate)
             return;
         lastUpdate = Time.time;
         numVertices = Mathf.RoundToInt(Vector3.Distance(startPos.position, beam.position) / keyVertexDist) * 3 + 1;
         Vector3 currentV = startPos.localPosition;
         Vector3[] keyVertices = new Vector3[numVertices];
         GetComponent<LineRenderer>().SetVertexCount(numVertices);
         for (int i = 0; i < numVertices; i++) {
             Vector3 v = new Vector3(
             currentV.x + (Random.value * 4.0f - 2.0f),
             currentV.y + (Random.value * 4.0f - 2.0f),
             currentV.z + (Random.value * 4.0f - 2.0f)
             );
             GetComponent<LineRenderer>().SetPosition(i, v);
             keyVertices[i] = v;
             currentV += (beam.localPosition - startPos.localPosition) / numVertices;
         }
         GetComponent<LineRenderer>().SetPosition(0, startPos.localPosition);
         GetComponent<LineRenderer>().SetPosition(numVertices-1, beam.localPosition);
         for (int i = 0; i < numVertices; i++) {
             if (i % 3 == 0)
                 continue;
             float x = (Vector3.Distance(keyVertices[i-1], keyVertices[i+2]) / 3);
             for (int j = 0; j < 2; j++) {
                 Vector3 v = new Vector3(
                 keyVertices[i-1].x + (Mathf.Sin(x-(Mathf.PI/2.0f))/2.0f+0.5f) * (keyVertices[i+2].x-keyVertices[i-1].x),
                 keyVertices[i-1].y + (Mathf.Sin(x-(Mathf.PI/2.0f))/2.0f+0.5f) * (keyVertices[i+2].y-keyVertices[i-1].y),
                 keyVertices[i-1].z + (Mathf.Sin(x-(Mathf.PI/2.0f))/2.0f+0.5f) * (keyVertices[i+2].z-keyVertices[i-1].z)
                 );
                 GetComponent<LineRenderer>().SetPosition(i+j, v);
                 x += x;
             }
             i += 2;
         }
     }
 }
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
1

Answer by zach-r-d · Jun 11, 2015 at 08:15 AM

You can set the points of an edge collider by assigning a Vector2[] to EdgeCollider2D.points. It looks like keyVertices pretty much has everything you need already, just need to convert to an array of Vector2s.

Remember to use transform.InverseTransformPoint to go from world coordinates to local coordinates if necessary!

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 pradip_spixel · Aug 06, 2018 at 06:27 AM 0
Share

assigning collider by points works on other objects when line collided to them... but when one line collision to other line collision not working... pls share any solutions

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

23 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

Related Questions

C# Invalid Points Assigned to 2D Edge Collider 1 Answer

C# EdgeCollider2D Points Not Updating Continuously 0 Answers

Need help fixing an error. Using raycast to mimic laster beam but no collider, Line Renderer, Raycast Collider. 1 Answer

Line Renderer and Collider Question 0 Answers

Multiple Cars not working 1 Answer


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