• 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 Wolfshadow · Nov 24, 2015 at 11:49 PM · 2diosslicingcutting

Cut a 2D model

Hello everyone, I am relatively new to unity, and, although I have done some research, none is useful, or presented in a way that I can comprehend. I want to do something like this: I found this on a similar question:  http://answers.unity3d.com/questions/379157/cutting-simple-plane-with-a-line-2d.html

Could someone show and explain implemented code? Thank you! By the way, I work in IOS, so if the code could be easily converted to touch, I would be grateful. I can convert.

6627-cuttingillustration.png (29.7 kB)
Comment
Add comment · Show 3
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 Wolfshadow · Nov 25, 2015 at 02:17 PM 0
Share

I will reward for any answers

avatar image Wolfshadow · Nov 25, 2015 at 03:04 PM 0
Share

Please help me out. if you have any knowledge, I can use it

avatar image Wolfshadow · Nov 28, 2015 at 10:52 PM 0
Share

Edit: sorry, by this I mean cut along the line

2 Replies

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

Answer by ___... · Nov 25, 2015 at 08:54 PM

here is a sample script (javascript):

FingerLine.js

pragma strict

@script RequireComponent(LineRenderer)

var lineRenderer : LineRenderer; var myPoints : Vector3[];

function Start () { lineRenderer = GetComponent(LineRenderer); lineRenderer.SetWidth(0.2,0.2); }

function Update () {

  if(myPoints){
      lineRenderer.SetVertexCount(myPoints.Length);
      for(var i = 0;i<myPoints.Length;i++){
          lineRenderer.SetPosition(i,myPoints[i]);    
      }
  }
  else
  lineRenderer.SetVertexCount(0);
  
  if(Input.touchCount > 0){
  if(Input.touches[0].phase == TouchPhase.Began)
      InvokeRepeating("AddPoint",.1,.1);
  } 
  else{
      CancelInvoke();
      myPoints = null;
  }

}

function AddPoint(){

  var tempPoints : Vector3[];
 
  if(!myPoints)
      tempPoints = new Vector3[1];
  else{
      tempPoints = new Vector3[myPoints.Length+1];
             
      for(var j = 0; j < myPoints.Length; j++)
          tempPoints[j] = myPoints[j];
  }
      var tempPos : Vector3 = Input.mousePosition;
  tempPos.z = 10;
  
 tempPoints[j] = Camera.main.ScreenToWorldPoint(tempPos);
 myPoints = new Vector3[tempPoints.Length]; 
 myPoints = tempPoints;   

} this pretty much works on its own, just attach to the camera of a new scene and build..

Enjoy!

Comment
Add comment · Show 4 · 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 Wolfshadow · Nov 28, 2015 at 09:01 PM 0
Share

And this script cuts a mesh or a sprite?

avatar image Wolfshadow · Nov 28, 2015 at 10:52 PM 0
Share

@__... I got the script to work, but it only draws a line, it does not cut. Why not? Can you explain a bit more what this script does? Thanks!

avatar image Eno-Khaon Wolfshadow · Nov 29, 2015 at 12:21 AM 0
Share

Well, it does only draw a line. That script is designed to keep track of consecutive finger positions (updated every 1/10 of a second) and draw a line between those points.

From there, you need a way of deter$$anonymous$$ing where the line crosses your object, if at all, as well as using that line to calculate a new pair of vertices.

You'll use that pair of vertices in a new pair of meshes (one per remaining piece of your object, and possibly more than just two if meshes or collision areas aren't square) and calculate a new arrangement of vertices per mesh. It's not easy because there's no guarantee you're not slicing off a triangular piece from the original mesh.

That said, it's a reason why @Jessespike's suggestion isn't bad. You either need to go all in on learning how to detect the details of the cut, the resulting meshes that would form, the triangle arrangement on the new meshes in order to properly display them, the new UV coordinates to properly display the textures on them, and then apply any resulting physical interactions to demonstrate that there are now multiple objects... or take the easier road and buy what's already completed.

avatar image Wolfshadow Eno-Khaon · Nov 29, 2015 at 05:49 PM 0
Share

alright, thank you for your time! I am very grateful.

avatar image
1

Answer by Jessespike · Nov 25, 2015 at 04:25 PM

The technique is called CSG (Constructive solid geometry) and it isn't trivial. There are references online that explain how it's done, but you'll have to implement it yourself in Unity. Mesh API would be the place to start. If you're new to Unity or programming as you claim, than you're probably better off using an existing asset on the store:

Turbo Slicer

Sprite Slicer

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

46 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

Related Questions

Render text on sprite prefab - 2D iOS 4 Answers

Does Unity take care of unseen objects? 2 Answers

Is unity the way to go for a 2D game for iOS? 2 Answers

IOS Swipe Gesture 0 Answers

Destroy Object On Collision? 3 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