- Home /
Can I have a moving shape as a trigger zone?
I have the following problem:
I want to have a first person player be able to interact with objects around the level. Nothing too complicated; when the player gets within range, and is looking at the object, they can press E and it will play a sound.
Someone recommended I use trigger zones around objects to detect when the player is within range. To me it would make more sense to add a cylindrical shape just in front of the player and have that be the trigger zone, rather than lots of different trigger zones.
Is it possible to do this? Can I attach a trigger to the player and have all game objects I want to interact with in an array?
I'm working in C# so if anyone has any code to help demonstrate their answer that would be great.
Answer by Mmmpies · Feb 07, 2015 at 02:30 PM
yes, this is for 3D setup as you didn't specify 2D or 3D.
Create an empty GameObject to your player and Add Component to that for a capsule collider.
Tick is trigger and resize so it's bigger than the player. You can move offsets to say where it appears. Move it so it's not triggered by the player, like this:
You should be fine with that. I tested it with a cube with a rigidbody that drops onto it.
using UnityEngine;
using System.Collections;
public class hitSomething : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
Debug.Log (other.name);
}
}
And the debug prints out cube.
Thanks for the answer. Out of curiosity, you mentioned that it works when you drop a cube onto the trigger zone, but does it work if you move the trigger zone into another object?
Yep, just added force to it and put the cube on the ground and it appears to. Looks pretty funny as it hits the cube and falls over like it had few too many drinks, then it reports the terrain triggers as well!
I'm sure you've got a more professional setup :¬) but are you having issues with it triggering?
Your answer
Follow this Question
Related Questions
Wanting audio to pause when entering a trigger zone 1 Answer
OnTriggerExit stop audio 1 Answer
how do i play a sound if i press a particular key? 2 Answers
Play AudioClip on trigger with First Person Controller 1 Answer
Audio Trigger 3 Answers