Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 UBrownie · Oct 27, 2020 at 11:15 AM · triggercollision detectioncollider2dlinedraw

Draw line within bounds of circular collider

Hello all!

I'm trying to draw a line within bounds of a circular object. I found some code online to draw line and with some edit I was able to limit the size of the line.

     private Vector3 pos1;
     private Vector3 pos2;
     public float objectHeight = 1.0f;
     public GameObject prefab;
 
     private GameObject go;
 
     private bool isBeingDrawn = false;
     private Collider2D collider2d;
 
     private void Start()
     {
         collider2d = GetComponent<Collider2D>();
     }
 
     private void OnMouseDown()
     {
         if (Input.GetMouseButtonDown(0))
         {
             pos1 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             pos1.z = -2f;
             pos2 = pos1;
 
             go = Instantiate(prefab, pos1, Quaternion.identity, GameObject.Find("empty").transform);
             Vector3 temp = go.transform.localScale;
             temp.y = 0.0f;
             go.transform.localScale = temp;
             isBeingDrawn = true;
         }
     }
 
     private void OnMouseUp()
     {
         isBeingDrawn = false;
     }
 
     void Update()
     {
         if (isBeingDrawn)
         {
             Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             if (pos.x < collider2d.bounds.extents.x && pos.y < collider2d.bounds.extents.y && pos.x > -collider2d.bounds.extents.x && pos.y > -collider2d.bounds.extents.y)
             {
                 pos2 = pos;
                 pos2.z = -2f;
             }
         }
 
         if (pos2 != pos1)
         {
             Vector3 v3 = pos2 - pos1;
             go.transform.position = pos1 + (v3) / 2.0f;
             go.transform.localScale = new Vector2(go.transform.localScale.x, v3.magnitude);
             go.transform.rotation = Quaternion.FromToRotation(Vector2.up, v3);
         }
     }

But the line still moves outside the circular object specially at the corners as you can see in the pictures.

alt text

alt text

How can I keep it within the collider bounds? any help is appreciated.

scene.png (17.3 kB)
game.png (15.5 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

140 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

Related Questions

Free script/plugin for drawing a line with a collider? 2 Answers

OnTriggerEnter2D being called by GameObject without the trigger 1 Answer

Setting a Prefab Clone as the Child of another Object on Collision (2D) 1 Answer

Can more than one colliders be used for different purposes..???? 1 Answer

What other reasons would a 2D collider not make contact with another 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