Wednesday, May 19, 2010

Scene One: How it all works & changes.

Just thought I'd put a quick post in on how scene one is setup in Unity and discuss the scripts I have used.

Below is an image of the scenes setup:


The four big rectangles are my four triggers, and the red line shows the waypoints the dodo follows. There is also three cameras, camera one is a third person camera targeting the dodo, camera two has a script attached which caused it to look at the dodo as it flies, and finally a fixed camera which shows the dodo crashing.

My first trigger switches the view from camera one to camera two. The script it uses to do this is the cross-fade script mentioned in my last post which I have modified to work when OnTriggerEnter is fired as below.


var camera1 : Camera;
var camera2 : Camera;
var fadeTime = 2.0;
private var inProgress = false;
private var swap = false;
function OnTriggerEnter (collision : Collider)
{
DoFade();
}

function DoFade () {
if (inProgress) return;
inProgress = true;

swap = !swap;
yield ScreenWipe.use.CrossFade (swap? camera1 : camera2, swap? camera2 : camera1, fadeTime);

inProgress = false;
}


The second trigger switches the view from camera two back to camera one with the same script as above.

The third trigger switches the camera from camera one to camera three and changes the dodo's animation to the crash animation. This is done by modifying my crossfade camera's trigger script as above to have an extra variable which is a gameobject.

var animateobject: GameObject;


Once I attach this script to the trigger I can then set animateobject to the instance of my dodo. I can then use the following line to switch the animation my dodo is playing with this line in OnTriggerEnter:
animateobject.animation.CrossFade("crash");


My final trigger took me a considerable amount of time to figure out. Unfortunately once I attached a rigid body to my dodo so it would cause a trigger event when passing through triggers it would no longer stop when it ran out of waypoints. As a result my dodo would keep going beyond the last waypoint until it hit the volcano and then it would bounce all over the place. After alot of googling and trial and error I came up with the solution of removing the rigid body once my dodo hit the final trigger with the following very simple line:
Destroy (other.rigidbody);


This stops my dodo moving but plays out the last of the crash animation. This trigger also cause's a cube to fall out of a tree. This cube represents a fruit which I will model down the track which will fall on the dodo when it crashes in the tree. To trigger the fruit to fall the cube has a rigidbody attached with gravity turned off. My final triggers script has the variable:
var fruit: GameObject;


When the trigger even is fired I simple enable gravity on this rigid body which causes it to fall with this line:
fruit.rigidbody.useGravity = true;


Overall I'm happy with how the scene is setup although it is obviously rough. The only changes I am going to make are to tweak the camera angles and to remove the 3rd person camera and replace it with scripted camera's which move as desired around the dodo so I don't need to manually move the camera's when recording the scene. My ultimate goal is to have the scene scripted to play from start to finish without my input. This will make it nice and easy next semester for me to update models an recapture the entire movie.

No comments:

Post a Comment