Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question My GetMouseOver Function Isn't Working (And Yes, I have Tried The Inbuilt Function OnMouseOver)

Discussion in 'Scripting' started by Sooly890, Mar 21, 2023.

  1. Sooly890

    Sooly890

    Joined:
    Aug 24, 2022
    Posts:
    107
    Always returns false. Code:
    Code (CSharp):
    1.     bool GetMouseOver()
    2.     {
    3.         RaycastHit hitInfo;
    4.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    5.             if (mc.Raycast(ray, out hitInfo, Mathf.Infinity))
    6.             {
    7.                 return(true);
    8.             }
    9.             else
    10.             {
    11.                 return(false);
    12.             }
    13.     }
    result: https://imgur.com/a/6bB0RrF
     
  2. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Hi, what is mc.Raycast on line 5? Physics.Raycast should be sufficient.

    Not sure about the messages I see in your video, how do you know it's returning false? Is that the Mouse Exit we see being printed in the console?

    Your raycast looks ok otherwise. What's the rest of the script look like?
     
  3. Sooly890

    Sooly890

    Joined:
    Aug 24, 2022
    Posts:
    107
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MeshControl : MonoBehaviour
    6. {
    7.     private Camera cam;
    8.  
    9.     [HideInInspector]
    10.     public List<Vector3> grid;
    11.  
    12.     public Material[] materialsList;
    13.  
    14.     [SerializeField]
    15.     private Object node;
    16.     [SerializeField]
    17.     private Object MeshController;
    18.  
    19.     private Vector3[] vertices;
    20.     private Vector3[] normals;
    21.     private int[] triangles;
    22.     private Mesh mesh;
    23.     private MeshFilter mf;
    24.     private MeshRenderer mr;
    25.     private MeshCollider mc;
    26.  
    27.     private Vector3[] nodePlace;
    28.  
    29.     [HideInInspector]
    30.     public int i;
    31.  
    32.     private bool clone;
    33.  
    34.     void Start()
    35.     {
    36.         cam = Camera.main;
    37.         mc = GetComponent<MeshCollider>();
    38.         mr = GetComponent<MeshRenderer>();
    39.  
    40.         if (gameObject.name.Contains("(Clone)"))
    41.         {
    42.             clone = true;
    43.         }
    44.         else
    45.         {
    46.             clone = false;
    47.         }
    48.         if (clone == false)
    49.         {
    50.             grid = new List<Vector3>();
    51.             AddMesh(new Vector3(0, 0, 0), new Vector3(3, 1, 3), new Vector3(4, 0, 4),2);
    52.             AddMesh(new Vector3(0, 0, 0), new Vector3(3, 2, 3), new Vector3(3, 0, 5),1);
    53.             StartCreatingMeshs();
    54.         }
    55.         else
    56.         {
    57.             LoadMeshs();
    58.         }
    59.     }
    60.  
    61.     void Update()
    62.     {
    63.         if(clone)
    64.         {
    65.             if (GetMouseOver())
    66.             {
    67.                 Instantiate(node, nodePlace[0], transform.rotation);
    68.                 Instantiate(node, nodePlace[1], transform.rotation);
    69.                 Instantiate(node, nodePlace[2], transform.rotation);
    70.                 Debug.Log("mouseOver");
    71.             }
    72.             else
    73.             {
    74.                 Debug.Log("mouseExit");
    75.             }
    76.         }
    77.     }
    78.  
    79.     void BuildMesh(Vector3 vec1, Vector3 vec2, Vector3 vec3, float mat)
    80.     {
    81.         int a = i - 1;
    82.  
    83.         mr.material = materialsList[(int)mat];
    84.  
    85.         vertices[0] = vec1;
    86.         vertices[1] = vec2;
    87.         vertices[2] = vec3;
    88.  
    89.         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))));
    90.         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))));
    91.         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))));
    92.  
    93.         triangles[0] = 0;
    94.         triangles[1] = 1;
    95.         triangles[2] = 2;
    96.  
    97.         mesh.vertices = vertices;
    98.         mesh.normals = normals;
    99.         mesh.triangles = triangles;
    100.  
    101.         mf = GetComponent<MeshFilter>();
    102.         mf.mesh = mesh;
    103.  
    104.         nodePlace = new Vector3[3];
    105.         nodePlace[0] = vec1;
    106.         nodePlace[1] = vec2;
    107.         nodePlace[2] = vec3;
    108.  
    109.     }
    110.  
    111.     float GetDir(Vector2 pos)
    112.     {
    113.         if (pos.y < 0)
    114.         {
    115.             return (Mathf.Atan(pos.x / pos.y) + 180f);
    116.         }
    117.         else
    118.         {
    119.             return (Mathf.Atan(pos.x / pos.y) + 0f);
    120.         }
    121.     }
    122.  
    123.     void AddMesh(Vector3 vec1, Vector3 vec2, Vector3 vec3, float mat)
    124.     {
    125.         grid.Add(vec1);
    126.         grid.Add(vec2);
    127.         grid.Add(vec3);
    128.         grid.Add(new Vector3(mat, 0, 0));
    129.     }
    130.  
    131.     void LoadMeshs()
    132.     {
    133.         mesh = new Mesh();
    134.         vertices = new Vector3[3];
    135.         normals = new Vector3[3];
    136.         triangles = new int[3];
    137.  
    138.         BuildMesh(grid[i*4-4], grid[i*4-3], grid[i*4-2], grid[i*4-1].x);
    139.  
    140.     }
    141.  
    142.     void StartCreatingMeshs()
    143.     {
    144.         i = 0;
    145.  
    146.         for (int a = 0; a <= grid.Count / 4; a++)
    147.         {
    148.             i++;
    149.             if (i - 1 < grid.Count / 4)
    150.             {
    151.                 Instantiate(MeshController);
    152.             }
    153.         }
    154.         Destroy(mc);
    155.         Destroy(mr);
    156.         Destroy(mf);
    157.     }
    158.  
    159.     void ClearAll()
    160.     {
    161.         GetComponent<MeshFilter>().mesh.Clear(false);
    162.     }
    163.  
    164.     bool GetMouseOver()
    165.     {
    166.         RaycastHit hitInfo;
    167.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    168.             if (mc.Raycast(ray, out hitInfo, Mathf.Infinity))
    169.             {
    170.                 return(true);
    171.             }
    172.             else
    173.             {
    174.                 return(false);
    175.             }
    176.     }
    177. }
    178.  
    EDIT: only worry about "void Start" and "void Update"