- Home /
The question is answered, right answer was accepted
OnTriggerStay instead of input
Is there a way I can change the "if(Input.GetMouseButton(0))" to, onTriggerStay? Basically I want one animation to play on triggerstay, and onTriggerExit I want the second animation to play. Ex: A char walking into a sphere collider for the trigger? Not exactly sure if a player/char should be called somewhere
void Update ()
{
if(Input.GetMouseButton(0))
{
Ray ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit rayCastHit;
if(Physics.Raycast(ray.origin, ray.direction, out rayCastHit, Mathf.Infinity))
{
Monster monster = rayCastHit.transform.GetComponent<Monster>();
if(monster)
{
monster.PlayMonsterAnim();
}
}
}
}
This the anim.script
private int m_LastIndex;
public void PlayMonsterAnim ()
{
if(!animation.isPlaying)
{
if(m_LastIndex == 0)
{
animation.Play("moveOut");
m_LastIndex = 1;
}
else
{
animation.Play("moveIn");
m_LastIndex = 0;
}
}
}
I would suggest moving your code to the collider events
void OnTriggerStay(Collider other) {} void OnTriggerExit(Collider other) {}
I'm not exactly sure what you mean , could you explain a lil more? or help with example
Answer by Mmmpies · Mar 09, 2015 at 10:19 AM
OnTriggerStay is an overhead all you need to do is set a bool with OnTriggerEnter to True and OnTriggerExit to False.
private bool IveBeenTriggered = false;
void OnTriggerEnter(Collider other)
{
IveBeenTriggered = true;
}
void OnTriggerExit(Collider other)
{
IveBeenTriggered = false;
}
// Then where you want your animation to play:
if(IveBeenTriggered && !animation.isPlaying("moveIn"))
{
animation.Play("moveIn");
}
else if (!animation.isPlaying("moveOut")
{
animation.Play("moveOut");
}
Although it's not really clear what triggers the animation currently anything that enters that collider will.
Hey man sorry for the late reply, had to sleep lol. I am wondering where the animation should be called. Update perhaps? i asked because the "monster" isnt activated until you click on another game object. And I want the monster to pop out when the player walks near. Would using update be proper? or will that be using unneeded resources?
im getting the error, " Unexpected symbol - line 20" when I put it in update, so, i guess not hah
$$anonymous$$inda depends on what you're doing with the animation but if you just want the moveIn animation to play once when OnTriggerEnters you could just put it in the trigger and put the moveOut in the OnTriggerExit. No need for the bool then.
Or, if you want to check every frame you could put it in Update if that's easier and check that bool.
It's Unity so there's always 10's if not 100's of ways of doing things.
Chances are just playing the animation in the OnTriggerEnter/Exit will be good enough but depends on animation looping and what you want to do when the animation ends for either one.
Glad it helped @Ouija and no problem, still learning loads of stuff on here myself so maybe 1/2 a year to a year ahead of you. Gets easier doing simple stuff but man it's a big subject.
O$$anonymous$$, sorry for the late response and my next might not be too fast either, but are you setting this up to use the animation to move the character out and back in?
It can be done that way so you effectively end up with a cuckoo clock type effect, say you had a cube in a cave that when the trigger Enters animates out and when the trigger exits goes back in.
But, if you are using animations like this, there are better ways. I'll see if I can setup a test scene later with some free assets to show you a simple State $$anonymous$$achine but you really want to gen up on $$anonymous$$ecanim as it's ideal for easy state machines.
Answer by kevinspawner · Mar 09, 2015 at 09:20 AM
You can create a triggerstay and trigger exit function and use bool to switch between the animation in the trigger function.
I wrote this, is it close to what you were talking about? if so, i'm still unsure how to set up the ontriggerstay /exit tho lol
public void Play$$anonymous$$onsterAnim (bool something)
{
if(something) {
if(!animation.isPlaying)
{
if(m_LastIndex == 0)
{
animation.Play("moveIn");
m_LastIndex = 1;
}
else
{
animation.Play("moveOut");
m_LastIndex = 0;
}
}
}
}
Follow this Question
Related Questions
Trigger animation going crazy 3 Answers
MonoBehaviour.OnTriggerX vs Collider.OnTriggerX 1 Answer
Simple AI and Animation 2 Answers
How to use OnTriggerEnter with multiple triggered objects? 1 Answer