Hi, In some cases we can have the need to implement triggering to give some "action" to the game, for example, an automatic door opening, a zombie to appear, or even deactivation of unnecessary objects to the passage of avatar. I will illustrate this torurial as a capsule appears at the passage of avatar. Instead of the reappearance of a capsule, we can make disappear a false floor to make de avatar fall into a whole, the imagination is the limit.
I leave a 5 minutes video of how it's done the whole process and the steps are as follows:
- New Terrain
- New Sphere
- New avatar
- Centering the avatar
- Remove the component "mesh renderer" of Sphere object
- Activate the "edit collider" the object Sphere
- Extend the desired area of contact
- New capsule
- In assets create new script in C# named "activarCapsule.cs"
- Edit with 2 clicks on the file activarCapsule.cs
- Set up a kind of input variable "gameObject" called capsule:
public GameObject Capsule;
- At start procedure deactivate the capsule to not appear at the beginning of the game:
void Start()
{
capsule.SetActive(false);
}
- At trigger enter procedure activate the object to appear when the colider is triggered:
void OnTriggerEnter(Collider other)
{
capsule.SetActive(true);
}
- Save the file activarcapsule.cs
- Drag activarcapsule.cs script as component into the Sphere object, this is the object which colider is used
- Drag the capsule object in to the variable Capsule in the script activarcapsule.cs added to the object sphere as a component.
- Reposition the camera inside the avatar
- Activate "is trigger" on the component "Sphere Collider" object "Sphere", without this option the collision area is not activated
When moving the avatar into to the area of collision the capsule apears.
To have different reactions simply drag objects or group of objects in to the variable Capsule.
You can also replace:
void OnTriggerEnter(Collider other)
{
capsule.SetActive(true);
}
by
void OnTriggerEnter(Collider other)
{
Application.LoadLevel("Save02");
}
With this change by passing the colider the game loads the schene "Save02" previously recorded and compiled (builded), a way to pass a game level or even a door opening.
Link of online build: http://jmvale.esy.es/Unity5/ToturialMeshCollider/
Source code: http://jmvale.esy.es/Unity5/ToturialMeshCollider.zip
Júlio Vale