- Home /
Want a script that can instantiate an object when MOVING inside the trigger
I so far have a script for spawning a prefab when you enter a trigger, only issue is I want it to spawn when im MOVING inside of the trigger. So it's somewhat like OnTriggerStay, but only activates when the player moves around. Tried OnMove, doesn't work.
It sounds like you know what needs to be done. Check if something is moving and check if something is inside of a trigger. Combine them?
Doing rigidbody.velocity, yet stil comes out with a bunch of errors
Have you read what the errors are telling you? Rigidbody.velocity should work just fine, so long as the character actually has a rigidbody. Another option could be to record the position of the character when they first enter the trigger, then, on the next frame, check how far the character's new position is from that first one. If it's too far, they've moved.
Answer by tategarringer · Sep 24, 2018 at 05:38 PM
Following along with the suggestion of @TreyH , it may be worth your time to go over some of the provided tutorials for Unity, as they briefly cover a section about boolean animation triggers that you could combine with the OnTriggerStay()
event to accomplish exactly what you're looking for. Establishing this movement boolean is best practice as you'll certainly have more than one scenario where you'd want to check if the player is moving, so allowing movement to set it to true or false takes care of a lot of code redundancy. I digress, example for the question provided,
void OnTriggerStay(Collider other)
{
if(animator.GetBool("isMoving"))
{
///Do Stuff;
}
}
Your answer
Follow this Question
Related Questions
OnTriggerEnter2D crashing editor 0 Answers
Using triggers to repeat basic scenes 1 Answer
Problems with the Trigger Collider (randomly fictional) 1 Answer
Enemy instantiate script, vars not showing in Inspector and error I can't fix 1 Answer
How to trigger animaton on two different Game Object's at the same time? 1 Answer