Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 darkpredator1919 · Jan 26, 2017 at 05:15 PM · 2d gamecollider2dball

How to create a collider between 2 points in a 2d game?

I am creating a 2D game in which a ball rolls on a line drawn by the user. I tried to create the line and the colliders in the same time, but the collision is applying only in points, not between them. The ball gets stuck in this space or just goes through the line. I know there is a way to create a collider with the dimension between the points, but I am a beginner and I have problems with the syntax.

Here is the code that creates the line and some colliders:

void Update() {

     if (Input.GetMouseButtonDown(0))
     {
         createLine();
         isMousePressed = true;
         line.SetVertexCount(0);
         pointsList.RemoveRange(0, pointsList.Count);
         line.SetColors(startColor, endColor);

     }


     // Drawing line when mouse is moving(presses)
     if (isMousePressed)
     {
         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         mousePos.z = 0;



         if (!pointsList.Contains(mousePos))
         {
            
            

             pointsList.Add(mousePos);
             line.SetVertexCount(pointsList.Count);
             line.SetPosition(pointsList.Count - 1, (Vector3)pointsList[pointsList.Count - 1]);


             if (pointsList.Count > 2)
             {
                 Vector3 l = pointsList[pointsList.Count - 2];
                 Vector3 k = pointsList[pointsList.Count - 1];
                 print(Vector3.Distance(k, l));
                 if (k != l)
                 {
                     GameObject colliderKeeper = new GameObject("collider");
                     BoxCollider2D bc = colliderKeeper.AddComponent<BoxCollider2D>();
                     colliderKeeper.transform.position = Vector3.Lerp(k, l, 0.5f);
                     colliderKeeper.transform.LookAt(k);
                     bc.size = new Vector3(0.1f ,0.1f, Vector3.Distance(k, l));
                 }

             }

         }


     }

     if (Input.GetMouseButtonUp(0))
     {
         lines.Add(line);
         Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         Vector3 newDotPosition = mouseRay.origin - mouseRay.direction / mouseRay.direction.y * mouseRay.origin.y;
         isMousePressed = false;


     }


 }


Any help is appreciated.

Comment
Add comment · Show 1
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 darkpredator1919 · Jan 26, 2017 at 06:18 PM 0
Share

@Scroodge$$anonymous$$ Could you help me please? I know you made some code that looks approximately the same.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by GarretLawrence · Jan 27, 2017 at 12:12 AM

There is EdgeCollider component that can solve your problem.

I made a simple demo, you can check it

You can move the anchors in scene mode while game is running to see the collider change.

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 darkpredator1919 · Jan 27, 2017 at 02:27 PM 0
Share

Thanks a lot! I understood how EdgeColiider works from your code and now I can use it.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

BoxCollider2D have an offset on collision,Collider2D has an collision offset? 0 Answers

Polygon Collider 2d doesnt work properly 3 Answers

Is there a way of casting Rays at the end of the collider of an object ? 0 Answers

Tag and Collider problem 1 Answer

How to make projectiles bounce between enemy? 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