- Home /
Enable ChildObject of Player with OnTriggerEnter? Or a whole different method?
Hi there.
I've been looking around for quite some time now, but can't seem to find a solution.
What I'd like to do is enable a Text when the player enters a TriggerCollider, then have it destroy itself after a few seconds. That Text also needs to have a fixed position to the player and follow him around, as if it was a dialogue coming from his mouth.
So far I have a GameObject attached as a child to the Player named "Dialogue 1". It contains the following:
a Mesh Renderer
a Text Mesh
a script named DontRotateWithPlayer.js, which currently makes it not rotate, but wont have the text put in a certain position. If the player turns around, the Text moves a little to the left. This script also destroys the GameObject after some seconds.
DontRotateWithPlayer.js
#pragma strict
var destroyTime=3 ;
function Start ()
{
yield WaitForSeconds(destroyTime);
Destroy(gameObject);
}
function Update()
{
//SOMETHING'S WRONG HERE. IT STILL MOVES AROUND WHEN THE PLAYER ROTATES!!
transform.rotation = Quaternion.identity;
transform.parent = transform;
transform.localPosition = new Vector3(0f, 0f, 0f);
}
Next to the player is a GameObject that's supposed to activate the trigger, named "Dialogue". It has the following components:
a Box Collider with "Is Trigger" activated.
a Mesh Renderer thats inactive.
a script named DisplayDialogue.js I think this is the one causing me problems.
DisplayDialogue.js
#pragma strict
var Dialogue : TextMesh;
function OnTriggerEnter()
{
Debug.Log("Enable the dialogue!!");
Dialogue.renderer.enabled = true;
}
Currently the plan is to have the "Dialogue 1" GameObject disabled from the beginning. Then, as soon as the player enters the trigger, the GameObject should activate and DontRotateWithPlayer.js would play itself, ending up destroying it.
But it doesn't.
What am I doing wrong?? Should I use GUI Text instead?
I'm also planning on adding this method to NPCS aswell, so they will be having dialogues played when the player enters a collider. Will this be the same method?