Use Tigger triggers to make interactive icons appear after approaching
This article is based on Unity2019.3.2f1 version
Recently I want to make a first-person game. The interaction between characters and objects is used in many games. When the characters are close to a certain distance, a prompt icon will appear on the top of the object, and then press a key according to the prompt. Object interaction. So with this article, record your own learning process.
Step 1: Make the objects to interact
Because it is a function, the physical objects are tried with ordinary cubes.
First create a Cube and an empty object Tigger with Box Collider, drag the empty object onto the Cube, and make the empty object belong to the child of the Cube. Remember to check the is Tigger of the empty object.
Step 2: Make a reminder icon
Just create a plane to live it up, then right-click in the file bar->Create->Material to create a new shader, hang the picture of your choice, and then resize it and hang it on the empty object with the trigger, here I am I went up with the letter E with P. After all, I got used to this way when I played a lot of things like old rolls. Here I made it into a prefab for the convenience of future use.
Step 3: Code
The code about triggers is easy to write, and there are special methods. Here to borrow the code of the big guys. Hang it on the empty Tigger object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TiggerTest : MonoBehaviour
{
public GameObject prompt;
public void Show(GameObject prompt){
//用来将某个物体激活或是禁用(这里是prompt,也就是那个图标
//禁用时这个物体和其子物体都会禁用,包括上面的脚本,在这里很方便
prompt.SetActive(true);
}
public void Hide(GameObject prompt){
prompt.SetActive(false);
}
void OnTriggerEnter(Collider other)//接触时触发,无需调用
{
Debug.Log(Time.time + ":进入该触发器的对象是:" + other.gameObject.name);
Show(prompt);
}
void OnTriggerStay(Collider other) //每帧调用一次OnTriggerStay()函数
{
Debug.Log(Time.time + "留在触发器的对象是:" + other.gameObject.name);
}
void OnTriggerExit(Collider other)
{
Debug.Log(Time.time + "离开触发器的对象是:" + other.gameObject.name);
Hide(prompt);
}
// Start is called before the first frame update
void Start()
{
Hide(prompt);
}
// Update is called once per frame
void Update()
{
}
}
As you can see, this script realizes that when an object enters the trigger, the icon will appear and disappear after leaving.
But when debugging, another problem appeared: the icon is just a plane after all, and the 2d paper icon looks very bad when the character moves in 3d.
So we are writing a script that keeps the icon facing the camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FaceToCamera : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//当前对象始终面向摄像机。
this.transform.LookAt(Camera.main.transform.position);
this.transform.Rotate(90,0,0);
}
}
Then hang the script on the object plane icon.
Finally, because of the lighting, when the icon will appear backlit when it is rotated, then you need to modify the properties of the shader used by the icon and select the Shader as Unlit/Texture and you are done.
Results
In order to test whether it was successful, I made a simple dialogue with the Fungus plugin and hung up.
Enhanced Edition
Although the above solves the problem of being able to interact after getting close to the object, if you want to interact with small objects, such as taking away on the table or checking a small object such as a key cup, it is difficult to achieve, so you must think of a new The method triggers Tigger.
The method is actually very simple, just put a Cube without the Mash Renderer component on the camera.
Adjust the position appropriately
and you're done!
At this time, as the camera moves, the Cube will also move, and then when the trigger is touched, the script will be triggered to realize the function of interacting with the object.