- Home /
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:
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.
Please help me out. if you have any knowledge, I can use it
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!
@__... 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!
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.
alright, thank you for your time! I am very grateful.
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:
Your answer
Follow this Question
Related Questions
Type of graphics 1 Answer
iOS profiling help 0 Answers
Simultaneous Touch Drag Controls 0 Answers
2D Sidescroller: Change camera position? 1 Answer
How to I scale UI 9-slicing across different resolutions? 2 Answers