Highlight objects when they are interactable.

ryankopf

New member
Hey there!

Most RPG type games these days have something highlight objects you can interact with or pickup - either by highlighting the object or by displaying a little "F" key above the object.

Here's my code which generates a little cube above an object you can interact with. Later I'll make this do something line clone an "F" or "Interact" key or something. I'm sharing it because I am wondering if there's an easier way to do what I'm wanting to do here?

If not - I'd love to see this be a feature of the PickupItem ability, and I release this code under the public domain and the MIT License if you want to start somewhere.
~
PickupItem.cs

++ (insert after line 138, or before you call "m_AvailableItemPickups[m_AvailablePickupCount] = itemPickup;")
HighlightObjectForAbility(obj);

++ (insert before "m_AvailableItemPickups = null;")
UnhighlightForAbility(other);

++ (insert at bottom of the PickupItem class)
private void HighlightObjectForAbility (GameObject obj) {
for (int y = 0; y < obj.transform.childCount; ++y) {
GameObject child = obj.transform.GetChild(y).gameObject;
if (child.name == "Highlight") { child.SetActive(true); return; }
}
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.parent = obj.transform;
cube.transform.localPosition = Vector3.zero;
cube.transform.localScale -= new Vector3(0.8f, 0.8f, 0.8f);
cube.transform.position += new Vector3(0, 0.75f, 0);
cube.GetComponent<BoxCollider>().enabled = false;
cube.name = "Highlight";
}

private void UnhighlightForAbility(GameObject obj) {
for (int y = 0; y < obj.transform.childCount; ++y) {
GameObject child = obj.transform.GetChild(y).gameObject;
if (child.name == "Highlight") { child.SetActive(false); } //UnityEngine.Object.Destroy(child);
}
}
 
Top