• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

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

What other reasons would a 2D collider not make contact with another 1 Answer

Gameobject not detecting collison from other Box Collider 2D [SOLVED?] 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges