Saturday 9 April 2011

Rotating an Object around the character

The gravity shift simulation is a huge part of the gaming dynamic and I really wanted to get the basics of the script nailed down straight away.



After searching through Unity Script Reference, I came across the RotateAround function (http://unity3d.com/support/documentation/ScriptReference/Transform.RotateAround.html), which allowed me to rotate one object around the axes of another. Because I am going to rotate the level, rather than the player, or gravity direction this is exctly what I wanted to do. To test the function I created 2 basic game objects, a square and sphere. I then created a small script, which I attached to the sphere, which made it orbit around the cube. Here is the script:

function Update() {
transform.RotateAround (GameObject.Find("Cube").transform.TransformPoint(Vector3.zero), Vector3.right, 90 * Time.deltaTime);
}


To further develop this, I added a quick mockup of a testing ground, consisting of a large cube, with 4 sets of stairs going around the "room". I then added aThird Person Controller from the Unity Standard Assets and assigned a key code to initiate the rotation.


var shiftKey : KeyCode;

function Update() {
if( Input.GetKey( shiftKey) )
{
//rotate the world around the character, along the x axis
transform.RotateAround (GameObject.Find("3rd Person Controller/Bip001").transform.TransformPoint(Vector3.zero), Vector3.right, 90 * Time.deltaTime);
}
}

No comments:

Post a Comment