0
\$\begingroup\$

I have a Camera i added and put it above the space station. The Camera name is Map Camera. I attached to it a collider Capsule collider before it tried also with Box Collider.

Also attached to the Map Camera a script name Map:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Map : MonoBehaviour
{
    public Camera mapCamera;

    private Camera[] cameras;
    private Color startcolor;

    // Use this for initialization
    void Start()
    {
        cameras = Camera.allCameras;

        GameObject levels = GameObject.Find("_Level");
        foreach (Transform child in levels.transform)
        {
            child.gameObject.AddComponent<CapsuleCollider>();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            if (mapCamera.enabled == false)
            {
                foreach (Camera cam in cameras)
                {
                    cam.enabled = false;
                }
                mapCamera.enabled = true;
            }
            else
            {
                foreach (Camera cam in cameras)
                {
                    cam.enabled = true;
                }
                mapCamera.enabled = false;
            }
        }
    }

    void OnMouseEnter()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        foreach (RaycastHit hit in Physics.RaycastAll(ray))
        {
            if (hit.collider.name == "Corridor_Window_Part_05")
            {
                break;
            }
        }
    }

    void OnMouseExit()
    {

    }
}

In the Start I'm adding to each child also a collider. While the game is running i tried to change the colliders set Is Trigger to be on/off but nothing helped it's never getting to the OnMouseEnter event. I used a break point.

Screenshot of the Map Camera inspector.

gir

And this is a screenshot for example of the child Corridor_Window_Part_05 while the game is running so now it have a capsule collider too.

gir

What I'm trying to do is when the mouse cursor in the game window while the game is running will be over the child for example Corridor_Window_Part_05 then get into the OnMouseEnter but it's never get to the OnMouseEnter.

The last screenshot showing for example the Corridor_Window_Part_05 in the scene view and I want that in the game view while the game is running when i move the mouse cursor on one of any of the children under _Level to get into OnMouseEnter but it's never firing the event.

gir

Update on what I did so far:

Changed the Map script to:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Map : MonoBehaviour
{
    public Camera mapCamera;

    private Camera[] cameras;

    // Use this for initialization
    void Start()
    {
        cameras = Camera.allCameras;

        GameObject levels = GameObject.Find("_Level");
        foreach (Transform child in levels.transform)
        {
            child.gameObject.AddComponent<MapHighlight>();
            child.gameObject.AddComponent<BoxCollider>();
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            if (mapCamera.enabled == false)
            {
                foreach (Camera cam in cameras)
                {
                    cam.enabled = false;
                }
                mapCamera.enabled = true;
            }
            else
            {
                foreach (Camera cam in cameras)
                {
                    cam.enabled = true;
                }
                mapCamera.enabled = false;
            }
        }
    }
}

And created new script called Map Highlight:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapHighlight : MonoBehaviour
{
    private Color startcolor;

    private void OnMouseEnter()
    {
        startcolor = GetComponent<Renderer>().material.color;
        GetComponent<Renderer>().material.color = Color.white;
    }

    private void OnMouseExit()
    {
        // Remove the white color.
    }
}

The Map script is attached to the Map Camera. When running the game I see on the children objects the script and the box collider i added.

I have a box collider also on the Map Camera.

But when the mouse cursor is over one of the children nothing happen. I'm using a break point on:

startcolor = GetComponent<Renderer>().material.color;

In the Map Highlight script inside the OnMouseEnter but it's never getting there.

I tried to change the Is Trigger on/off on the children and on the map camera i tried to remove the box collider from the map camera then only from one of the children and then from both nothing worked so far.

It's not getting to the OnMouseEnter.

Could be the mouse cursor is too far ?

\$\endgroup\$
2
\$\begingroup\$

You have to put the OnMouseEnter on the object you wish to BE moused over, so in your case, the station object.

\$\endgroup\$
  • \$\begingroup\$ I did. But still not working. I will update my question with what i did. Could be the mouse cursor is too far ? \$\endgroup\$ – jdoedoe doedoe Jan 18 '18 at 22:19
  • \$\begingroup\$ Im pretty sure objects attached to the active camera are excluded from that event. \$\endgroup\$ – Stephan Jan 18 '18 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.