示例#1
0
    private void Update()
    {
        Vector2 mousePos = Mouse.current.position.ReadValue();

        Vector2 scaledMousePos = new Vector2(mousePos.x / Screen.width, mousePos.y / Screen.height);

        Shader.SetGlobalVector("_MousePos", scaledMousePos);
        if (Physics.Raycast(cam.ScreenPointToRay(mousePos), out RaycastHit hit, 100))
        {
            if (hit.collider == null)
            {
                if (lastHovered != null)
                {
                    lastHovered.Reset();
                    lastHovered = null;
                }
                return;
            }

            if (hit.collider.gameObject.TryGetComponent <LiftOnHover>(out LiftOnHover hithoverer))
            {
                if (hithoverer != lastHovered)
                {
                    lastHovered?.Reset();
                    hithoverer.Lift();
                    lastHovered = hithoverer;
                }
            }
            else if (lastHovered != null)
            {
                lastHovered.Reset();
                lastHovered = null;
            }
        }