Possible improvement for functionality when clicking on a Waypoint

I found that instead of using OnMouseOver and checking Input.GetMouseButtonDown, it could be replaced with OnMouseUpAsButton without checking the Input.

It functions as a left-click input without having to deal with any of the Input functionality and only triggers over the collider you click on. So, for example, if you held the click down, moved away from that object and then released it, the event wouldn’t fire. I’m not sure if the way it’s coded in the lecture would have the same functionality or not but that is desired functionality for my project so I figured I’d use OnMouseUpAsButton. It’s also not called every frame, like OnMouseOver, which I would think would improve performance but that’s just a guess.

I’m not sure if OnMouseUpAsButton could be modified if you wanted to have the click be from a different input, like right-click, where using OnMouseOver and checking the Input can since you provide which input to test.

I haven’t looked into this extensively and I could’ve missed something in this/previous lectures as to why this approach was taken. Or maybe I’ll learn more about it in future lectures and I have something to look forward to. Either way any feedback is welcome.

I copied the lecture code and mine below for comparison along with the link to the documentation for OnMouseUpAsButton.

Lecture Changes:

void OnMouseOver()
{
    print(gameObject.name);
    if (Input.GetMouseButtonDown(0)) // left click
    {
        if (isPlaceable)
        {
            print(gameObject.name + " tower placement");
        }
        else
        {
            print("Can't place here");
        }
    }                
}

My Changes:

void OnMouseUpAsButton()
{
	if (m_bIsPlaceable)
		print( name + " - Can place Tower here" );
	else
		print( name + " - Can't place Tower here" );
}

Link - https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseUpAsButton.html

1 Like

Privacy & Terms