- Home /
Using direction/angle to change animation?
This is for a 2D infinte runner - I asked a previous question, but thought about it some more and have come up with a fairly vague answer that I'm not too sure about. I have a player (see diagram) that when he jumps he might miss the obstacle and play his animation regardless (which is what I don't want).
So what I was thinking - what if I had a raycast pointing toward his nearest obstacle, retrieving that angle (or mathematical function), and if it's within a threshold of whatever angle I set it will play a specific animation. This way it might not play the animation if he will miss the obstacle. Is something like this possible? Or is there something easier?
Some additional info - player's x and z values are frozen (he only jumps). Obstacles constantly fly at him.
Answer by Nazirzadeh · May 10, 2016 at 08:01 PM
Check collision for your obstacle, if it is collided then play your specific animation, also you can put your obstacle inside an invisible box collider , then you can define boundary for your collision and check the collision in that boundary.
@$$anonymous$$J$$anonymous$$C this is what I have above. Boundary? Having a boundary is essentially redundant if I already have a collider isn't it?
You do not want to miss the obstacle, am I right? Suppose that player jumps over the obstacle and pass it without any collision. How do you want to notice that?
I think your boundary should be a little bigger than your obstacle, which your player cant jump over it. Not too much big.
It seems you already have a Trigger collider. Does it work at all?
I am using boundaries for playing animations like yours and it works perfectly, I check OnTriggerEnter() function to notice the overlap with boundaries.
@$$anonymous$$J$$anonymous$$C There might be some solution...Yes - I have a trigger on it, and it does its job. It works perfectly, but then it plays again, or it's delayed. I think what the problem is is that OnTriggerEnter2D might not be the best solution (nor OnTriggerExit), because I want him to play the animation while hitting the object, and not playing the animation while missing the object. I would have to post a gif or video to better describe this situation, and I might just post it on the forums side.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080536im_/https://answers.unity.com/themes/thub/images/avi.jpg)