Jump to content

  • Log In with Google      Sign In   
  • Create Account





Genesis Phase II complete - physics on the ship to station collision

Posted by IcedCrow, 29 July 2013 · 429 views

The screenshot remains the same as in phase 1, only this time we added some physics to the mix to bounce the ship off of it. This took a little bit of playing around…

Posted Image

The first thing that I had to do was get my ship model loaded in. I’m using the basic ship from the Direct X SDK that Microsoft provides. This is just a basic model… added a reflective material to the surface. Spent time positioning the camera over the cockpit (and in the bottom render you can see the nose of the craft) and then added mesh colliders to both the ship and the space station.

The majority of this phase was figuring out Unity and how to get colliders to work.
I set the station and the ship to both have a collider, to have rigid bodies, and to not be kinemetic. This was very important… as this is how the OnCollision trigger is fired. If the colliders are kinemetic then you must use the trigger properties instead, which I chose to not implement because I actually want my ship to be able to collide with things and not necessarily destroy them.

Once that was done I created a new script called “ShipPhysics” and added that to my ship (the motor script is attached to the camera, that may be changed in the future but for right now the camera still controls the movement of the parent body). I inserted a simple event handler that would check the tag of the object I collide with, and if its a space station, to run a custom method to end my app if my current speed is over 40m/s.
private void OnCollisionEnter(Collision collision)
{
//rigid bodies collided
if (collision.gameObject.tag == TAG_STATION)
{
CollideWithSpaceStation();
}
}
private void CollideWithSpaceStation()
{
//if current speed is slow, nudge the station
//if current speed is not slow, the ship will be destroyed for right now
if (this.GetComponentInChildren<Ship>().CurrentSpeed > 40)
{
Debug.Log (“Quitting”);
Application.Quit ();
}
}
Testing the code confirms that it works. I can now fly around the station and run into it and it bounces my ship off of it, and if over 40 m/s will end the app (blowing me up)

Now I need to start constructing a GUI…




October 2014 »

S M T W T F S
   1234
567891011
12131415161718
192021222324 25
262728293031 

Recent Comments

PARTNERS