Always returns false. Code:
bool GetMouseOver()
{
RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (mc.Raycast(ray, out hitInfo, Mathf.Infinity))
{
return(true);
}
else
{
return(false);
}
}
result: Imgur: The magic of the Internet
rest of code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshControl : MonoBehaviour
{
private Camera cam;
[HideInInspector]
public List<Vector3> grid;
public Material[] materialsList;
[SerializeField]
private Object node;
[SerializeField]
private Object MeshController;
private Vector3[] vertices;
private Vector3[] normals;
private int[] triangles;
private Mesh mesh;
private MeshFilter mf;
private MeshRenderer mr;
private MeshCollider mc;
private Vector3[] nodePlace;
[HideInInspector]
public int i;
private bool clone;
void Start()
{
cam = Camera.main;
mc = GetComponent<MeshCollider>();
mr = GetComponent<MeshRenderer>();
if (gameObject.name.Contains("(Clone)"))
{
clone = true;
}
else
{
clone = false;
}
if (clone == false)
{
grid = new List<Vector3>();
AddMesh(new Vector3(0, 0, 0), new Vector3(3, 1, 3), new Vector3(4, 0, 4),2);
AddMesh(new Vector3(0, 0, 0), new Vector3(3, 2, 3), new Vector3(3, 0, 5),1);
StartCreatingMeshs();
}
else
{
LoadMeshs();
}
}
void Update()
{
if(clone)
{
if (GetMouseOver())
{
Instantiate(node, nodePlace[0], transform.rotation);
Instantiate(node, nodePlace[1], transform.rotation);
Instantiate(node, nodePlace[2], transform.rotation);
Debug.Log("mouseOver");
}
else
{
Debug.Log("mouseExit");
}
}
}
void BuildMesh(Vector3 vec1, Vector3 vec2, Vector3 vec3, float mat)
{
int a = i - 1;
mr.material = materialsList[(int)mat];
vertices[0] = vec1;
vertices[1] = vec2;
vertices[2] = vec3;
normals[0] = new Vector3(Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))), Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.y - vec2.y))), Mathf.Cos(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))));
normals[1] = new Vector3(Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))), Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.y - vec2.y))), Mathf.Cos(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))));
normals[2] = new Vector3(Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))), Mathf.Sin(GetDir(new Vector2(vec1.x - vec2.x, vec1.y - vec2.y))), Mathf.Cos(GetDir(new Vector2(vec1.x - vec2.x, vec1.z - vec2.z))));
triangles[0] = 0;
triangles[1] = 1;
triangles[2] = 2;
mesh.vertices = vertices;
mesh.normals = normals;
mesh.triangles = triangles;
mf = GetComponent<MeshFilter>();
mf.mesh = mesh;
nodePlace = new Vector3[3];
nodePlace[0] = vec1;
nodePlace[1] = vec2;
nodePlace[2] = vec3;
}
float GetDir(Vector2 pos)
{
if (pos.y < 0)
{
return (Mathf.Atan(pos.x / pos.y) + 180f);
}
else
{
return (Mathf.Atan(pos.x / pos.y) + 0f);
}
}
void AddMesh(Vector3 vec1, Vector3 vec2, Vector3 vec3, float mat)
{
grid.Add(vec1);
grid.Add(vec2);
grid.Add(vec3);
grid.Add(new Vector3(mat, 0, 0));
}
void LoadMeshs()
{
mesh = new Mesh();
vertices = new Vector3[3];
normals = new Vector3[3];
triangles = new int[3];
BuildMesh(grid[i*4-4], grid[i*4-3], grid[i*4-2], grid[i*4-1].x);
}
void StartCreatingMeshs()
{
i = 0;
for (int a = 0; a <= grid.Count / 4; a++)
{
i++;
if (i - 1 < grid.Count / 4)
{
Instantiate(MeshController);
}
}
Destroy(mc);
Destroy(mr);
Destroy(mf);
}
void ClearAll()
{
GetComponent<MeshFilter>().mesh.Clear(false);
}
bool GetMouseOver()
{
RaycastHit hitInfo;
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
if (mc.Raycast(ray, out hitInfo, Mathf.Infinity))
{
return(true);
}
else
{
return(false);
}
}
}