- Home /
Answer by Mike 3 · May 18, 2010 at 03:13 PM
Something along the lines of this:
function OnTriggerEnter()
{
if (action == Action.Enable)
{
target.animation.Play();
}
else if (action == Action.Animate)
{
target.audio.Play();
}
}
Edited so that it'll work no matter which component you use for target
It should just work in your script (I'm not sure what the exact variables are and if you're using enums for the actions, but those should be fixable easily)
Edit 2, using your own code... Edited it a little so that it's less prone to breaking:
using UnityEngine;
public class ActivateTrigger : MonoBehaviour { public enum Mode { Trigger = 0, // Just broadcast the action on to the target Replace = 1, // replace target with source Activate = 2, // Activate the target GameObject Enable = 3, // Enable a component Animate = 4, // Start animation on target Deactivate= 5 // Decativate target GameObject }
/// The action to accomplish public Mode action = Mode.Activate;
/// The game object to affect. If none, the trigger work on this game object public Object target; public GameObject source; public int triggerCount = 1;/// public bool repeatTrigger = false;
void DoActivateTrigger() { --triggerCount;
if (!(target is Component || target is GameObject)) return;
if (triggerCount == 0 || repeatTrigger) { GameObject targetGameObject = target.gameObject;
switch (action)
{
case Mode.Trigger:
targetGameObject.BroadcastMessage ("DoActivateTrigger");
break;
case Mode.Replace:
if (source != null)
{
Object.Instantiate (source, targetGameObject.transform.position, targetGameObject.transform.rotation);
DestroyObject (targetGameObject);
}
break;
case Mode.Activate:
targetGameObject.active = true;
break;
case Mode.Enable:
if (target is Behaviour)
((Behaviour)target).enabled = true;
break;
case Mode.Animate:
targetGameObject.animation.Play ();
break;
case Mode.Deactivate:
targetGameObject.active = false;
break;
}
}
} void OnTriggerEnter (Collider other) { DoActivateTrigger (); } }
this solution dont work..how to set this script or the activate trigger script can you make a concrete example?
the animation target is: messoplayer the animation is: watertouch the soundsource is sound1 the sound is audioprova
thank you
edited, if it still doesn't work you'll need to paste the activate trigger script - that's as close as i can get it without seeing it
here's the script.. http://forum.unity3d.com/viewtopic.php?p=320196#320196
thank you!
Answer by gianni123 · May 22, 2010 at 10:33 AM
animation solved:
var animateobject: GameObject;
function OnTriggerEnter (collision : Collider)
{
if (collision.gameObject.tag == "Player") //
{
animateobject.animation.CrossFade ("animationname");
} }
audio solved:
using the "activate trigger" script with replace option