Hi guys,
I have a new question related to 2D Physics. In the following image, you can see a paddle and two balls:
Let’s imagine that the paddle moves to the right and the balls move in a right-down direction. The behavior I have currently implemented is as follows:
- The ball located over the paddle will collide with the surface and bounce off the paddle. To detect this collision, I am casting the ball collider in the direction of its motion and as soon as the paddle is detected I am calculating the new direction of the ball.
- On the other hand, the second ball, the one to the right of the paddle, will be hit by the paddle. The idea is that the velocity of the paddle will be added to the velocity of the ball, pushing it a little until the ball moves away, and the collision is no longer detected. To detect this collision, I am casting the paddle collider in the direction of paddle motion.
As you can imagine, these two collision detections occur in two different FixedUpdate methods of two different scripts, so the order is important, because I need to know if the balls are being hit by the paddle so that its final direction of motion is affected.
The solution seems easy: I add both scripts to the Script Execution Order list giving priority to the paddle over the ball, but I feel this may not be a good practice. So my question after this long explanation is: is there any alternative to avoid using Script Execution Order or is it ok to use it in cases like this?
As an alternative, I was thinking of creating a CollisionManager where I first move the paddle and then calculate all possible collisions, first the paddle and then the ball, so I avoid any lack of synchronization.
Any other ideas? Thank you very much in advance!
For what it’s worth, I prefer [DefaultExecutionOrder(###)] - an attribute set above the class - rather than Script Execution Order.
They accomplish the same thing, but I’m less likely to forget that I’ve set Default Execution Order because I can see it whenever I open that class. You can also look up all references to DefaultExecutionOrder within your code editor in case there’s any question, which is faster than within Unity.
Manually moving the paddle in a Collision Manager sounds more complex than adjusting the order, so I would start w/ changing the order and proceed from there.
2 Likes
Thank you for your answer, Lo-renzo.
I was not aware of this possibility. And if I use this on two of my classes, do I have to set the DefaultExecutionOrder on all other existing classes? Or will the ones without this attribute use a default value (e.g. [DefaultExecutionOrder(0)])?
However, this seems to be similar to using SEO in the Editor, so I still have a question: is this a good practice? It seems like my code will be very tight to the script order, but there may not be a better option… I’m just being a bit annoying with this question because I’d like to know if there is an alternative that I may not be aware of.
Yes, all non-attributed scripts have a default order of zero. Or are set explicitly in SEO.
It’s not something to introduce if there’s no need for it. Ideally, the update order doesn’t matter … but sometimes it does.
If you have a few systems that are coupled together, you could explicitly call one thing then another thing then another from a single script. That might be clearer in some cases. For instance, I’ve had issues where I wanted to make sure AI logic → Pathing logic → other logic, so instead of using SEO/ExecutionOrder I called them in order from a single class that owned the Update().
1 Like