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
0
Question by mikejgrecojr · Mar 05, 2014 at 03:03 PM · 2dtexture2dspriterenderergetpixelpolygoncollider2d

Get set of Pixels from PolygonCollider2d Position

Hello, This is my first question so I apologize in advance if I break any rules.

I am using the Unity 4.3.4, 2D project.

I'm working on a piece of code that gets triggered when a "Shot" hits a "Mountain". What I want to achieve is the Shot creating a crater in the Mountain. I have a CircleCollider2d on the Shot and a PolygonCollider2d on the Mountain. I am entering when the event occurs with the following code, which is working fine:

 void OnTriggerEnter2D(Collider2D otherCollider)
 {
     //Check if our Shot hit the Mountains
     if (otherCollider.name == "Mountains") 
     {
         deformPixels(otherCollider.gameObject,gameObject.transform.position);
     }
     
     Destroy (gameObject);
 }
 


The problem comes from the deformPixels function. Right now I have the following:

 void deformPixels(GameObject land, Vector3 position)
 {
 
     Vector2 hitPos = new Vector2 (position.x, position.y);
 
     hitPos = convertToLocal(land.transform, hitPos);
 
     //Get the SpriteRenderer on the Mountains
     
     SpriteRenderer sr = land.GetComponent<SpriteRenderer> ();
 
     if (sr != null) 
     {
         //Size of the Texture
         Rect spriteRect = sr.sprite.textureRect;
 
         Vector2 landScale = new Vector2(land.transform.localScale.x,land.transform.localScale.y);
         
         //Adjusted Dims:
         //        dimension of the instantiated sprite?
         //        Get this by multiplying the instantiated Scale and the texture dimensions.?
         //        Not sure if this is correct...
         Vector2 adjustedDims = new Vector2(spriteRect.width * landScale.x,spriteRect.height * landScale.y);
 
         Debug.Log("SpriteRect = " + spriteRect + " Scale = " + landScale + " Adjusted Dims = " + adjustedDims + " HitPos = " + hitPos);
 
         Texture2D tex = sr.sprite.texture;
         
         Vector3 localPos = land.transform.InverseTransformPoint(position);
         Vector2 texturePos = new Vector2(localPos.x, localPos.y);
 
         texturePos.x = texturePos.x * tex.width;
         texturePos.y = texturePos.y * tex.height;
 
         Debug.Log("Texture Pos = " + texturePos);
 
         texturePos.x = texturePos.x / spriteRect.width;
         texturePos.y = texturePos.y / spriteRect.height;
         Debug.Log ("Texture Pos = " + texturePos);
     }
 }
 

The following is list of psuedo steps that I believe need to happen.

 /*
         Psuedo-psuedo:
         
         1. Get location of The shot on the Mountain
         2. Convert that location into texture coordinates of the Mountain
         3. Grab a portion of Pixels from the Mountain Texture based on were the shot lands
         4. Remove a subset of the grabbed pixels
         5. Re-write those pixels to the instantiated sprite
         6. Remove / Re add PolygonCollider2d to update collider
 
 */

I don't believe that I am doing this correct since my Mountain texture is 640X400 and the Scale is X = 3.4, Y = 3.4 Yet I am getting positions of + and - 2000 on the X axis. Which is Double what I would expect (3.4 * 640 / 2 = 1088 in + or - direction).

So far I can get the location of the hit on the mountain. I guess the most immediate thing that needs to happen is getting the location of the hit on the texture, so then I can get the set of pixels on the texture.

I have attached some screen shots:

Shot Before Land

Crater Created after shot would land

shotland2.png (33.1 kB)
shotland3.png (32.1 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

20 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

Related Questions

Texturing custom 2d sprite 0 Answers

How to create texture from multiple sprite in unity2d? 0 Answers

Weird Light Projector Rendering on 2D Sprite 1 Answer

Can't create a 2D sprite with Sprite.Create when creating texture in code 1 Answer

Why Polygon Collider 2D is becaming convex? 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