How to shoot cannonballs (How do rigidbodies work in Yahaha?)
I will shortly explain a bit how the rigidbodies work so you can apply this information broadly. So even if you’re not making a cannon, this info can be useful. I’ve tried to keep it simple, but it can be a complex subject. If you have any questions just ask me.
Rigidbodies, how do they work?
Rigidbodies are components you can add to objects. They make the object respond to physics. So you can make them move by bumping into them, they fall because gravity, you can give them mass etc. just like real world objects. Cannonballs need this, so go ahead and add the rigidbody component to your cannonballs.Build cannon, how?
Use pipe assets to put ammunition balls in, have a "spring" object that moves quickly, have a reload mechanism that moves aside so the balls fall into the cannon and have a pipe that directs the balls. Now connect the spring and reload move component to a trigger object (I used a button).You might’ve already tried to build a similar cannon and have noticed that it doesn’t seem to work. It probably looks similar to this:
Y no move?
What is happening here is the rigidbody has gone to sleep. When rigidbodies lay still they go "to sleep" for optimization purposes. They will wake up when another rigidbody touches it. This is the reason the platform goes through the ball. The platform doesn't have a rigidbody and thus can't wake up the rigidbody. That's why you see it move when the other ball touches it.I’m not a Yahaha dev, but this is what I think is going on: The rigidbody and collider are turned off when they aren’t moving. The object has a sneaky secret script attached, that says: “Moving? Ok, turn on rigidbody, otherwise turn it off”
So in this .gif it’s turned off, it’s not even influenced by gravity at this point. Another collider is then turned on. This collider uses layers that tell the collider what can touch it and what can’t. The player character is included in this layering system, as are other rigidbody objects, but normal objects (like walls, floors, random assets) aren’t, so they go right through and can’t wake up the rigidbody.
Logical, because otherwise they would never go to sleep, as they were probably already touching a floor before they went to sleep.
That’s why the spring object, the one that’s supposed to send the balls flying, goes right through.
In the future, I hope the Yahaha devs add a second collider, that’s slightly smaller, inside the current one. This one CAN touch other objects, but it wouldn’t be touching the ground, because it’s slightly smaller. So when a moving platform touches it, it still wakes up.
So what you do to work around this is, you make sure the object will never go to sleep. I did this by adding a platform underneath that keeps moving. Note that I also keep the ammunition above awake with another jiggling platform:
Y no shoot?
As you can see in this .gif, the ball STILL doesn’t shoot away. This is because rigidbodies and the move component use two different systems for movement. The rigidbody can inherit forces that are pushing on it. But the platform isn’t adding a force to the rigidbody, the platform is simply changing its coordinates every frame and the cannonball is being “pushed” aside with it, simply changing position. This doesn’t happen in the real world, so it can be tricky to grasp. Don’t worry too much about it, just know this: if a rigidbody detects movement from another object WITHOUT a rigidbody, it will not register as force, but as a change in position without force.
So to work around this, you need another rigidbody moving against this rigidbody, using force. This can be anything, a cube, another sphere or whatever. As long as it has a rigidbody. I solved this by simply using the next cannonball.
This work how?
So why is this working? The first ball gets hit by the second ball. They BOTH have rigidbodies, so the system registers this as actual force. Since it's moving so fast, it registers it as a LOT of force.Cannon tips
If you want to make your cannon precise, make sure the pipe you're using has almost no extra space. Don't make it too tight otherwise the balls won't move. If you want to make your cannon a bit more random, make it a little wider. Don't make it too wide, otherwise the balls might bump around inside the cannon too much and end up going in weird directs and/or not flying that far.You can also add a physic material component. This gives you the option to make your objects bouncy/slippery or not.
To sum up:
- Build your cannon
- Keep the rigidbodies moving
- Shoot a rigidbody by using another rigidbody object as a spring
- Enjoy your flying balls