Author Topic: Is it possible to hide a unit (GameObjectAnimator)?  (Read 328 times)

devCoderZero

  • Newbie
  • *
  • Posts: 37
    • View Profile
Is it possible to hide a unit (GameObjectAnimator)?
« on: April 17, 2019, 10:42:29 AM »
Hello,

I have a classical "unit marker" (circle around the unit) that I want to remove when a unit is deselected.

Is it possible to hide the "unit marker", so it does not show up on the map? Or is it possible to remove it from the map without destroying it? The only way I have found so far is to use the parent, child functions and destroy the parent, but that would also destroy my "unit marker" object.

Best regards!

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 4887
    • View Profile
Re: Is it possible to hide a unit (GameObjectAnimator)?
« Reply #1 on: April 17, 2019, 12:56:41 PM »
HI,

There's two ways to control an unit visibility.

1) Classic gameObject.SetActive() method. Example: gameObjectAnimator.gameObject.SetActive(false). This will hide the unit and any children. It will also disable the GameObjectAnimator component as well as other script (as expected).

2) Use GameObjectAnimator.visible property. Setting this property will hide/show the unit on the map but the gameobject continues active (ie. can move while invisible).

In latest beta you can use the GameObjectAnimator.OnVisibleChange event which fires each time an unit changes its visibility. So for instance, if you have some other objects (like a circle marker) that depends on the unit you can do this:

Code
anim.OnVisibleChange += (anim) => myCircleMarker.SetActive(anim.isVisibleInViewport);

Where anim is the GameObjectAnimator component attached to the unit.
The above code adds a hook to the OnVisibleChange event which executes the SetActive method on myCircleMarker. The isVisibleInViewport property of the GameObjectAnimator returns true if the unit is currently visible in the map. The difference between isVisibileInViewport and visible properties is subtle but important. The "visible" property is an attribute you specify, you control, while isVisibleInViewport is a state field, it can be changed by the asset depending if the unit is visible or goes out of screen while moving for example.

Note: the OnVisibleChange event is only available in latest beta.

devCoderZero

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: Is it possible to hide a unit (GameObjectAnimator)?
« Reply #2 on: April 17, 2019, 01:28:16 PM »
Thanks!

Brian

  • Guest
Re: Is it possible to hide a unit (GameObjectAnimator)?
« Reply #3 on: May 08, 2019, 11:05:24 AM »
Thanks for providing an answer for this issue.  However, I have tried setting the “visible” property for the GameObjectAnimator both in code and during runtime in the editor and it never works for me (both in my own project and in Veiwport example 04).

Also, in VS “visible” is shown as a field and not a property.

Has anyone been able to get this to work?

Thanks in advance for any help with this.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 4887
    • View Profile
Re: Is it possible to hide a unit (GameObjectAnimator)?
« Reply #4 on: May 08, 2019, 11:54:33 AM »
Are you using latest version from the Asset Store?

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 4887
    • View Profile
Re: Is it possible to hide a unit (GameObjectAnimator)?
« Reply #5 on: May 08, 2019, 02:06:19 PM »
I did a quick test of the "visible" property of the GameObjectAnimator and it seems to work. Maybe you have a specific case for which it doesn't behave as expected? Please make sure you're using latest version.
Here's the test video:
https://youtu.be/UK2h506MYQc

Brian

  • Guest
Re: Is it possible to hide a unit (GameObjectAnimator)?
« Reply #6 on: May 09, 2019, 02:21:05 AM »
Thank you for responding.  I’m embarrassed to say that I didn’t have the lasted version.  I updated to the latest (or so I thought) but for some reason it was still on an older rev.  Updated again and it works just as described.

Thank you!

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 4887
    • View Profile
Re: Is it possible to hide a unit (GameObjectAnimator)?
« Reply #7 on: May 09, 2019, 02:34:41 AM »
No problem, glad you got it working :)