Ouija
1
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;
}
}
}
You can create a triggerstay and trigger exit function and use bool to switch between the animation in the trigger function.
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.