Sunday 10 April 2011

snapping the direction

Next I needed to create a function to return the general direction of the character, so that the rotation of the world "snaps" to that particular direction. To try and find the rotation value of the character when, facing forward, left, right and backwards I created a bit of script to return that value.

print(GameObject.Find("3rd Person Controller/Bip001").transform.rotation);

The script

This returned the following values:
forward "-0.5, 0.5, 0.5, 0.5"
left "-0.7, 0.0, 0.0, 0.7"
back "0.5, 0.5, 0.5, -0.5"
right "0.0, 0.7, 0.7, 0.0"

Initially I tried to work away I could use these values in an if function to determine the direction of the character, however these are very hard to make sense of and much more complicated than the standard 360 degree rotation values.

After doing some searching in the Unity Script Reference, I discovered that the reason that they are hard to understand is because these are internal values given for rotation, used within Unity, called quaternions (http://unity3d.com/support/documentation/ScriptReference/Transform-rotation.html)

In actual fact, I was looking for a Euler (x, y, z, 0-360) position and by adding ".eulerAngles" to the end of "...transform.rotation" I received the following, more understandable values:

forward "270, 90, 0"
left "270, 0, 0"
back "270, 270, 0"
right "270,180, 0"

Now that I had the values for the 4 directions I want to use in the game's gravity shift system, I needed to create the function to recognise the direction the character is facing most. This means that if the character is facing directly forwards or 45 degrees to the left or right of forwards, the character is still recognised s being "forward".

I made a quick note as a reference for what I wanted to classify as forward, right, back and left. Because only the Y axis value changes between each direction, I only needed to access the Y value for the function.

if y >> 45 but << 135 forward
if y>>135 but << 225 right
if y>>225 but << 315 back
if y>>315 or << 45 left

the reference note

Before creating the if function itself I wanted to create a variable for the player's Y position so I didn't have to have unnecessary amounts of code duplicated.

Then, using my reference note I constructed a function to make Unity print a message, which says what direction the character is facing.






//create variable for gravity key
var shiftKey : KeyCode;

function Update() {

////create variable for player's Y rotation
var playerY : int = (GameObject.Find("3rd Person Controller/Bip001").transform.rotation.eulerAngles.y);

////check if gravity key is pressed
if( Input.GetKey( shiftKey) )
{
////rotate the world forward
transform.RotateAround (GameObject.Find("3rd Person Controller/Bip001").transform.TransformPoint(Vector3.zero), Vector3.right, 90 * Time.deltaTime);
}
////function to work out the general direction that the character is facing
if (playerY >= 45 && playerY <= 135)
{
print ("forward, " + playerY);
}
else if (playerY >= 135 && playerY <= 225)
{
print ("right, " + playerY);
}
else if (playerY >= 225 && playerY <= 315)
{
print ("back, " + playerY);
}
else
{
print ("left, " + playerY);
}
}


The entire script

No comments:

Post a Comment