Hey guys! I’m sort of new to Unity, most of my background comes from web development and simple mapping in UDK, Source, and now Unity. I was hoping someone would be able to help me with a physics script.
The idea is that you have a ball. I need the ball to kind of float in a 2D space (an x and y). Objects moving around would of course change the ball’s direction and velocity. The best way I can think of explaining it is like Airhockey, but with a sphere for a puck and no table. When you turn the table on, the puck starts to float around because of the air vents, and of course, the same physics would apply to things the puck ran into. I’m just stuck on where I would start. Any help is greatly appreciated. Thanks!
if you made a table of sorts, similar to a air hockey table. Made it’s material with no friction and the ball with no friction, it would act exactly like a air hockey table. To be a little more realistic you could have a random air current affect it every random(0.5 to 1.0) seconds. just a slight nudge in a direction.
The only thing you would have to do to make it look the way you are refering is to turn the renderer off on the barriers. You can also view it top down and not really have to mess with the x, y thing, it would be x, z then.
You could make it a 3d air hockey game using a static or close to static camera at the players point of view against a computer player.
Good luck with it.
Thank you so much. This has been a great deal of help at understanding how I could possibly pull this off. I’m still pretty new to the concept of actual mechanics, I’m usually the one setting things in place while someone else manipulates the way it works.
My vision for camera functionality would be kind of a snap to the object when selected. The camera would then lock to the object and follow it as it floats or when struck. Don’t be alarmed, the object serving as the puck wouldn’t be moving too fast from the perspective of the player so you probably wouldn’t feel sick while playing.
The playing field would be pretty much like a flat surface as I described earlier, lots of obstacles to dodge while your object is floating(kind of like Osmos) and when a floating object interacts with another object, it would transfer it’s energy into bumping the object away or in extreme cases, destroying itself or the object it collided with.
First, Osmos is a very spiffy looking game. It is a lot like the first part of Spore.
OK, for your task, you will need to constrain all of your objects to zero Y. This will center all of your objects on the same plane In your Update, you would want to make any given object’s Y velocity zero. In the Late Update, you would want to force the Y position to zero. By doing this you can use other shapes, not just spheres.
And turn gravity to Vector3.zero… 
Similar to Osmos:
You will need a OnCollisionEnter event that says, if the collision’s magnitude (how fast the collision occurred) Destroy the smaller object (object with less mass) and add it to the object that hit it. Then, increase the localScale by localScale of the smaller object.
Destroy on Collision:
In the OnCollisionEnter, simply ask, does this object have greater mass then the other, if it does, destroy the other.
Do not… do the reverse, if the object is smaller, let it be destroyed. So your not double adding mass or double destroying.
In Osmos they go a little step further. If one object is larger, it reduces the size of the smaller object and adds the difference to the size of the larger object. once the smaller object reaches a certain size, it is removed.
You’re amazing! I figured out the whole Y velocity thing, that was the easy part since I figured objects of different sizes would naturally be bumped off into a 3rd plane unless perfectly centered on a plane. I’m going to work on this over the next few days/weeks, whenever I get the chance to bust my ass. Hopefully we see some nice results and I can share what we’ve done with the community. Thanks a bunch, B!
If you want your pseudo hockey puck to appear to float, make the visible mesh a child of the rigidbody you’re running the physics with. Then when you turn on the ‘air vents’ simply offset the child’s transform a bit along the Y axis. Possibly even a small sin wave oscillation script for added effect.
Another tip for games like this. Scale up everything in the scene so your smallest moving rigidbody is a decameter or meter in size. Due to floating point imprecision, small objects have a tendency to pass right through colliders. If you use gravity at all, you’ll have to scale that up accordingly.
For your playfield and it’s barriers, use box colliders and make then rather thick along the axis of the puck’s travel. Fast moving objects can move past a thin collider in one physics timestep and thus never register a collision.
Increasing the physics timestep helps as well.
Good luck with your game!