- Home /
N00b question: how to implement direct transform manipulation collisions?
Hi everyone.
I'm getting to know unity and I've reached a small bump... On my test project I've implemented a "ship" object (behaves a bit like an airplane), a "track" (plane) and obstacles (cubes).
The goal is to make the "ship" fly around, while not being able to fly across the plane (track/floor) and the obstacles, my intention is to make it crash spectacularly when it collides, forcing the player to avoid them.
The ship's movement is done by direct transformation/manipulation of the ship object, since I wasn't able to find a suitable unity script (like the character controller).
This, as you can imagine, made a mess of my collisions:
-With gravity enabled on the obstacle plus a box collider and a box collider on the ship, the can't fly through the obstacle (sucess!), but it flyies trough the track (fail!).
-If I add a box collider and a rigidbody to the track, and enable gravity, the track "falls down to infinity" (fail!)
-If i set a mesh collider on the ship and activate convex, it will collide with the track, but it will also be "unflyable", since it just keeps spinning out of control like a possessed paper plane...
So my questions are the following:
1-Is there a Unity script I can use to make my movement script? Or should I take the highroad and "expand" the provided Move script (thus solving all my collision troubles because I wouldn't be using transform manipulation anymore)?
2-Alternatively, how can I configure my "track" (plane) object so that it stops "falling to infinity" when gravity is applied and start acting like a proper floor?
3-I've considered that maybe I'm approaching the problem in a very wrong angle (since my work so far is a mashup of unity examples and "creative" code), can anyone provide a recepie(or pointers) for properly setting up the environment that I need (floor, obstacle, flying thing)?
Thanks for helping out a poor n00b :)
Answer by Joshua · Feb 24, 2011 at 11:43 AM
Let's start with the plane. You want it to stop objects from falling through it but you don't want it to fall itself. Giving it a rigidbody with gravity disabled will do that.
Next make sure that all objects are above this plane when the game starts. If they are partly 'in' or 'through' the plane they will ignore it and all fall through as if it's not there.
Next give both the boxes and the player a rigidbody with gravity enabled. If everything works so far they should now stand on the floor/fall down and hit the floor.
Now go to the boxes and give each one of them a tag to identify them by. Call them 'obstacles' or whatever you'd like.
Now in script you have attached to your player to move him add the function
function OnCollisionEnter (collision : Collision){
if(hit.gameObject.tag == "obstacle"){
//the script that makes him do something, for instance makes him spin around the y-axis a couple of times the way you do in mariocart when you're hit. From your question I gather you want something to happen when they collide, this is where you do that
}
Because they are both rigidbodies they should also not be able to pass through each other. Hope this helped, I'm a unity newbie myself as well. :)
Hi and thanks for the help. Unfortunately it didn't quite work, the settings you've recommend make the box fall through the floor (and also the ship). I had to remove the rigidbodies and replace them by box colliders to prevent the obstacles from going through the floor. As for the ship, adding gravity totally wrecks my "transformation script" but adding a meshcollider to it without gravity seems to help... However this is far from perfect, any suggestions?