Warnings in games can be provided in many ways, for doing both the right and wrong things. However, having the option of 3D capabilities allows us to create more interesting and dynamic warning messages.
private
variable that will store the Animator component. To do this, let's write the following line:private Animator animator;
public
variables, since they are supposed to be set in the Inspector:public Transform player; public float activationDistance;
public
variables to set the name of the animation that we created before:public string appearingAnimation; public string disappearingAnimation;
bool
variable; it is used to trigger the animation. However, this is done only once and is used to distinguish between whether the warning is enabled or not:private bool isEnable;
Start()
function, we need to assign the value to the animator
variable. We can do this by getting the component:void Start () { animator = GetComponent<Animator> (); }
Update()
function, we have to check whether the distance between the warning and the player is less than activationDistance
. If so, it means that the player is close enough to trigger the warning. Then, we need to see the value of the isEnable
variable to check whether the warning is already activated. In fact, if it is not, then we set the isEnable
variable to true
and trigger appearingAnimation
. Otherwise, we do the opposite of this process. Thus, if the player is far enough, the warning is deactivated. In fact, we can set the isEnable
variable to false
and trigger disappearingAnimation
. Therefore, we can write this code:void Update () { if (Vector3.Distance (player.position, transform.position) < activationDistance) { if(!isEnable){ isEnable = true; animator.Play(appearingAnimation); } } else { if(isEnable){ isEnable = false; animator.Play(disappearingAnimation); } } }
After we have created our 3D warning along with its two animations and placed it in the environment, we have to create a script that makes it pop up when the player gets close to it. We need to trigger the animation once, using the bool
variable isEnable
.
The focus of the next sections is to show us how it is possible to improve our 3D warning message.
If we want to improve computational performance, we can consider transforming the Update()
function into a coroutine. We have discussed coroutines in greater detail in the previous chapter, so you can revisit it again, especially in the There's more... section of the first recipe.
Imagine that we are using the 3D warning to trigger a pop-up message for a pickup item on the floor. Since the player can reach it from different directions, it's not nice that when it comes up, it has a different orientation. So, until the warning for instance on the wall, the player can see it from only one direction and it doesn't need to face the player. But in other cases, this is necessary. In order to do so, we have to change our script a little.
Let's add the following line at the end of the Update()
function of our script:
transform.LookAt (player.position);
In this way, we force the warning to always face the player.
This UI element is supposed to be in the environment. An example of its use may be to indicate to a player an item to pick up. In this example, it is possible to have the UI element floating rather than static in the air. So, we can follow the steps in the Making a floating UI element recipe in Chapter 5, Decorating the UI. Contrary to what we did in the Adding a smooth tilt effect section, where the floating logic could still be set in just two dimensions, here we might want to extend the floating effect to the third dimension. This should be used with the Rotating along its axis section rather than the Always orienting the warning towards the player section.
Therefore, if we decide to implement this extension, we have to change FloatingUIScript
a bit by adding two variables:
public float zspeed, zAmplitude;
Also, change the Update()
function like this:
void Update () { rectTransform.localPosition = new Vector3(xAmplitude*Mathf.Sin(Time.time*xspeed), yAmplitude*Mathf.Sin(Time.time*yspeed), zAmplitude*Mathf.Sin(Time.time*zspeed)); }
In this way, we can change the position and value of the z axis as well. As a result, we are utilizing the third dimension.
OnClick()
event to trigger the animation. This is done by calling the Play()
function on the Animator component. In contrast, we called the function directly inside our script, but the way it works is the same. So, refer to these two recipes to handle the animation of the 3D warning.FloatingUIScript
. A good place to refresh our memories about how to do this is the Making a floating UI element recipe in Chapter 5, Decorating the UI.3.137.167.195