I want to make that only if i press the mouse button down none stop and then drag the mouse it will do something.
I tried this:
void Update ()
{
if (Input.GetMouseButtonDown(0) && drag == true)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, 1000.0f))
{
int hitTri = hit.triangleIndex;
//get neighbour
int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
Vector3 p1 = vertices[triangles[hitTri * 3 + 1]];
Vector3 p2 = vertices[triangles[hitTri * 3 + 2]];
float edge1 = Vector3.Distance(p0,p1);
float edge2 = Vector3.Distance(p0,p2);
float edge3 = Vector3.Distance(p1,p2);
Vector3 shared1;
Vector3 shared2;
if(edge1 > edge2 && edge1 > edge3)
{
shared1 = p0;
shared2 = p1;
}
else if(edge2 > edge1 && edge2 > edge3)
{
shared1 = p0;
shared2 = p2;
}
else
{
shared1 = p1;
shared2 = p2;
}
int v1 = findVertex(shared1);
int v2 = findVertex(shared2);
deleteSquare(hitTri,findTriangle(vertices[v1], vertices[v2], hitTri));
}
}
}
bool drag = false;
void OnMouseDrag()
{
drag = true;
}
But this is working when i click the mouse button. I want to make button press none stop and drag.
Wait do you want this to work if you press the button frequently , or just regular drag system (press down, drag, press up)?
If second , then why don’t you use only OnMouseDrag then?
void OnMouseDrag()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, 1000.0f))
{
int hitTri = hit.triangleIndex;
//get neighbour
int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
Vector3 p1 = vertices[triangles[hitTri * 3 + 1]];
Vector3 p2 = vertices[triangles[hitTri * 3 + 2]];
float edge1 = Vector3.Distance(p0,p1);
float edge2 = Vector3.Distance(p0,p2);
float edge3 = Vector3.Distance(p1,p2);
Vector3 shared1;
Vector3 shared2;
if(edge1 > edge2 && edge1 > edge3)
{
shared1 = p0;
shared2 = p1;
}
else if(edge2 > edge1 && edge2 > edge3)
{
shared1 = p0;
shared2 = p2;
}
else
{
shared1 = p1;
shared2 = p2;
}
int v1 = findVertex(shared1);
int v2 = findVertex(shared2);
deleteSquare(hitTri,findTriangle(vertices[v1], vertices[v2], hitTri));
}
}
Also, be careful. Your bool variable, drag. At the beginning, it is false. When you start to drag, it becomes true. Afterwards, it will never become false again, because you didn’t write it. You can use your code, just add at the end of Update function this: drag = false; (and remove IF Statement at the beginning). However, as I said, it is meaningless to use Update function, since we have OnMouseDrag. Use code I posted above.
romatallinn:
Wait do you want this to work if you press the button frequently , or just regular drag system (press down, drag, press up)?
If second , then why don’t you use only OnMouseDrag then?
void OnMouseDrag()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, 1000.0f))
{
int hitTri = hit.triangleIndex;
//get neighbour
int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
Vector3 p1 = vertices[triangles[hitTri * 3 + 1]];
Vector3 p2 = vertices[triangles[hitTri * 3 + 2]];
float edge1 = Vector3.Distance(p0,p1);
float edge2 = Vector3.Distance(p0,p2);
float edge3 = Vector3.Distance(p1,p2);
Vector3 shared1;
Vector3 shared2;
if(edge1 > edge2 && edge1 > edge3)
{
shared1 = p0;
shared2 = p1;
}
else if(edge2 > edge1 && edge2 > edge3)
{
shared1 = p0;
shared2 = p2;
}
else
{
shared1 = p1;
shared2 = p2;
}
int v1 = findVertex(shared1);
int v2 = findVertex(shared2);
deleteSquare(hitTri,findTriangle(vertices[v1], vertices[v2], hitTri));
}
}
Also, be careful. Your bool variable, drag. At the beginning, it is false. When you start to drag, it becomes true. Afterwards, it will never become false again, because you didn’t write it. You can use your code, just add at the end of Update function this: drag = false; (and remove IF Statement at the beginning). However, as I said, it is meaningless. Use code I posted above.
I tried to use only with the OnMouseDrag and it worked but i also got exception on the line:
Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
IndexOutOfRangeException: Array index is out of range.
cutHole.Update () (at Assets/MyScripts/cutHole.cs:119)
And if the whole code is in the Update function then i don’t get this exception.
That’s why i tried to use it with the bool variable.
Chocolade:
I tried to use only with the OnMouseDrag and it worked but i also got exception on the line:
Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
IndexOutOfRangeException: Array index is out of range.
cutHole.Update () (at Assets/MyScripts/cutHole.cs:119)
And if the whole code is in the Update function then i don’t get this exception.
That’s why i tried to use it with the bool variable.
The problem is not in the function. The problem, as it is written, in the array index. Unfortunately, I am not an expert in vertices and triangles (so, I can’t tell you whether this line should work or not ), but try to debug 2 things:
Then analyse the results. Is the index(1 debug) less than the length of vertices(2 debug). if not, then you should look at this problem rather than ignoring it. Why did you decide that this should work? Where did you take it?
Again, I do not know what is the solution, but maybe you should use this instead:
Vector3 p0 = vertices[hitTri * 3 + 0];
Actually, debug this value as well. See what it will show.
I apologise if I wasted your time with my advises.
romatallinn:
The problem is not in the function. The problem, as it is written, in the array index. Unfortunately, I am not an expert in vertices and triangles (so, I can’t tell you whether this line should work or not ), but try to debug 2 things:
Then analyse the results. Is the index(1 debug) less than the length of vertices(2 debug). if not, then you should look at this problem rather than ignoring it. Why did you decide that this should work? Where did you take it?
Again, I do not know what is the solution, but maybe you should use this instead:
Vector3 p0 = vertices[hitTri * 3 + 0];
Actually, debug this value as well. See what it will show.
I apologise if I wasted your time with my advises.
No need to apologise, sorry for the mess.
This is the tutorial i’m trying to make:
https://www.youtube.com/watch?v=z1r7VjgufJ8
I took the script from here it’s the tutorial official script:
http://files.holistic3d.com/yt/z1r7VjgufJ8.zip
The full script as i’m using it now:
using UnityEngine;
using System.Collections;
public class cutHole : MonoBehaviour {
// Use this for initialization
void Start () {
}
void deleteSquare(int index1, int index2)
{
Destroy(this.gameObject.GetComponent<MeshCollider>());
Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
int[] oldTriangles = mesh.triangles;
int[] newTriangles = new int[mesh.triangles.Length-6];
int i = 0;
int j = 0;
while (j < mesh.triangles.Length)
{
if(j != index1*3 && j != index2*3)
{
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
}
else
{
j += 3;
}
}
transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
this.gameObject.AddComponent<MeshCollider>();
}
int findVertex(Vector3 v)
{
Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
for(int i = 0; i < vertices.Length; i++)
{
if(vertices[i] == v)
return i;
}
return -1;
}
int findTriangle(Vector3 v1, Vector3 v2, int notTriIndex)
{
int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
int i = 0;
int j = 0;
int found = 0;
while (j < triangles.Length)
{
if(j/3 != notTriIndex)
{
if(vertices[triangles[j]] == v1 && (vertices[triangles[j+1]] == v2 || vertices[triangles[j+2]] == v2))
return j/3;
else if(vertices[triangles[j]] == v2 && (vertices[triangles[j+1]] == v1 || vertices[triangles[j+2]] == v1))
return j/3;
else if(vertices[triangles[j+1]] == v2 && (vertices[triangles[j]] == v1 || vertices[triangles[j+2]] == v1))
return j/3;
else if(vertices[triangles[j+1]] == v1 && (vertices[triangles[j]] == v2 || vertices[triangles[j+2]] == v2))
return j/3;
}
j+=3;
}
return -1;
}
void deleteTri(int index)
{
Destroy(this.gameObject.GetComponent<MeshCollider>());
Mesh mesh = transform.GetComponent<MeshFilter>().mesh;
int[] oldTriangles = mesh.triangles;
int[] newTriangles = new int[mesh.triangles.Length-3];
int i = 0;
int j = 0;
while (j < mesh.triangles.Length)
{
if(j != index*3)
{
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
newTriangles[i++] = oldTriangles[j++];
}
else
{
j += 3;
}
}
transform.GetComponent<MeshFilter>().mesh.triangles = newTriangles;
this.gameObject.AddComponent<MeshCollider>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown(0) && drag == true)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, 1000.0f))
{
int hitTri = hit.triangleIndex;
//get neighbour
int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
Vector3 p1 = vertices[triangles[hitTri * 3 + 1]];
Vector3 p2 = vertices[triangles[hitTri * 3 + 2]];
float edge1 = Vector3.Distance(p0,p1);
float edge2 = Vector3.Distance(p0,p2);
float edge3 = Vector3.Distance(p1,p2);
Vector3 shared1;
Vector3 shared2;
if(edge1 > edge2 && edge1 > edge3)
{
shared1 = p0;
shared2 = p1;
}
else if(edge2 > edge1 && edge2 > edge3)
{
shared1 = p0;
shared2 = p2;
}
else
{
shared1 = p1;
shared2 = p2;
}
int v1 = findVertex(shared1);
int v2 = findVertex(shared2);
deleteSquare(hitTri,findTriangle(vertices[v1], vertices[v2], hitTri));
}
}
drag = false;
}
bool drag = false;
void OnMouseDrag()
{
drag = true;
}
}
Ok i found the problem.
The exception is thrown if click on the same index(triangle) twice. Since the triangle was deleted and not exist when i click on that again it throw the exception.
I can’t click on empty place.
The solution is to add a check and check if hit.triangleIndex is not -1
Just added the line: if (hit.triangleIndex != -1)
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000.0f))
{
if (hit.triangleIndex != -1)
{
int hitTri = hit.triangleIndex;
//get neighbour
int[] triangles = transform.GetComponent<MeshFilter>().mesh.triangles;
Vector3[] vertices = transform.GetComponent<MeshFilter>().mesh.vertices;
Vector3 p0 = vertices[triangles[hitTri * 3 + 0]];
Vector3 p1 = vertices[triangles[hitTri * 3 + 1]];
Vector3 p2 = vertices[triangles[hitTri * 3 + 2]];
float edge1 = Vector3.Distance(p0, p1);
float edge2 = Vector3.Distance(p0, p2);
float edge3 = Vector3.Distance(p1, p2);
Vector3 shared1;
Vector3 shared2;
if (edge1 > edge2 && edge1 > edge3)
{
shared1 = p0;
shared2 = p1;
}
else if (edge2 > edge1 && edge2 > edge3)
{
shared1 = p0;
shared2 = p2;
}
else
{
shared1 = p1;
shared2 = p2;
}
int v1 = findVertex(shared1);
int v2 = findVertex(shared2);
//deleteSquare(hitTri,findTriangle(vertices[v1], vertices[v2], hitTri));
deleteTri(hit.triangleIndex);
}
}
}
Is that the right way to solve it, by checking if the index is not -1 ?