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 /
  • Help Room /
avatar image
0
Question by unity_865E76FCAAEEDB577C33 · Jun 01 at 04:02 AM · collision2drecttransformrect

Convert two contact points in world space to size width in rect

I have two contacts points inside a collision, and I have tried to transform these contacts in width of a new rect, for fake cut sprite purpose, I read all documentations about Rect, and Collision, and nothing helped me, can anyone give me a fresh breeze about that. this is the code:

 void OnCollisionEnter2D(Collision2D target)
     {
         
         // Make an empty list to hold contact points
         ContactPoint2D[] contacts = new ContactPoint2D[target.contactCount];
 
         // Get the contact points for this collision
         int numContacts = target.GetContacts(contacts);
 
         var sprite = GetComponent<SpriteRenderer>().sprite;
         var a = contacts[0].point;
         var b = contacts[contacts.Length - 1].point;
         Cut(a, b);
     }
 
 void Cut(Vector2 cutPositionA, Vector2 cutPositionB)
     {
         var sprite = GetComponent<SpriteRenderer>();
         Texture2D _texture = sprite.sprite.texture;
         Sprite newSprite = Sprite.Create(_texture, new Rect(cutPositionA.x, 0f, cutPositionB.x, 512f), new Vector2(0.5f, 0.5f), 512f);
 
         GameObject sprGameObj = new GameObject();
         sprGameObj.transform.localScale = sprGameObj.transform.localScale + new Vector3(1f, 1f, 0f);
         sprGameObj.AddComponent<SpriteRenderer>();
         SpriteRenderer sprRenderer = sprGameObj.GetComponent<SpriteRenderer>();
         sprRenderer.sprite = newSprite;
         sprGameObj.transform.position = new Vector3(cutPositionA.x, cutPositionA.y);
         sprRenderer.sortingOrder = 1;
         sprGameObj.AddComponent<BoxCollider2D>();
         var bc = sprGameObj.GetComponent<BoxCollider2D>();
         bc.offset = new Vector2(0f, 0.06f);
         bc.size = new Vector2(1f, 0.7f);
         var rb = sprGameObj.AddComponent<Rigidbody2D>();
         rb.drag = 2f;
         rb.angularDrag = 3f;
         rb.mass = 1f;
     }

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

185 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

how can we modify a rect position to folow the camera position 0 Answers

Place GameObject's rect 0 Answers

Recttransform keeps changing when I test or play game 1 Answer

Helpp !! Can't draw the rectangle on the gameobject 's positon 0 Answers

RectTransform.rect.size alternating values 0 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