I am trying to get this 2d sprite to have a soft white glow when hovering over it. Here is a screenshot of when it is Not hovered, and another screenshot of when it is hovered. As you can see, the materials are switching appropriately, but the sprite is not changing in game.
This is my code for the Hover Effect.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoverEffect : MonoBehaviour
{
private SpriteRenderer spriteRenderer;
private Material originalMaterial;
public Material SpriteMaterialTint; // Assign your hover shader material in the Inspector
void Start()
{
// Get the SpriteRenderer component attached to this object
spriteRenderer = GetComponent<SpriteRenderer>();
// Save the original material of the sprite
originalMaterial = spriteRenderer.material;
}
void Update()
{
// Get the world position of the mouse
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mouseWorldPos.z = 0; // Ensure we're in 2D space
// Check if the mouse is hovering over the sprite's collider
if (GetComponent<PolygonCollider2D>().OverlapPoint(mouseWorldPos))
{
// Apply the hover shader material if mouse is over the collider
ApplyHoverEffect();
}
else
{
// Revert to the original material when mouse is no longer over the collider
RemoveHoverEffect();
}
}
void ApplyHoverEffect()
{
// Only apply the hover material if it's not already applied
if (spriteRenderer.material != SpriteMaterialTint)
{
spriteRenderer.material = SpriteMaterialTint;
Debug.Log("Mouse is over the pillow. Hover effect applied.");
}
}
void RemoveHoverEffect()
{
// Only revert to the original material if it's not already reverted
if (spriteRenderer.material != originalMaterial)
{
spriteRenderer.material = originalMaterial;
Debug.Log("Mouse left the pillow. Hover effect removed.");
}
}
}
And this is the code for my shader effect.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;
public class MaterialTintColor : MonoBehaviour
{
private Material material;
private Color materialTintColor;
private float tintFadeSpeed;
private void Awake()
{
materialTintColor = new Color(1, 0, 0, 0);
SetMaterial(transform.Find("Body").GetComponent<MeshRenderer>().material);
tintFadeSpeed = 6f;
}
private void Update()
{
if (materialTintColor.a > 0)
{
materialTintColor.a = Mathf.Clamp01(materialTintColor.a - tintFadeSpeed * Time.deltaTime);
material.SetColor("_Tint", materialTintColor);
}
}
public void SetMaterial(Material material)
{
this.material = material;
}
public void SetTintColor(Color color)
{
materialTintColor = color;
material.SetColor("_Tint", materialTintColor);
}
public void SetTintFadeSpeed(float tintFadeSpeed)
{
this.tintFadeSpeed = tintFadeSpeed;
}
}
I am not sure what I am doing wrong. What am I missing?

