0
votes

I have a GameObject known as myCamera. I am trying to use the Euler Angle of said GameObject to determine in what direction the character should move. An example of the return values expected are located here:

if 0 Degrees x = 1; z = 0
if 90 Degrees x = 0; z = 1
if 180 Degrees x = -1; z = 0;
if 270 Degrees x = 0; z = -1;

The current code looks like this:

    // TempVar[0] = tempX; [2] = finalX; [1] = finalZ;
    myCameraRotation = myCamera.transform.rotation.eulerAngles;
    if (myCameraRotation.y > 180f)
    {
        tempVar[0] = (int)myCameraRotation.y - 360f;
        Debug.Log("Over 180 - New Value: " + tempVar[0]);
    }
    else
    {
        tempVar[0] = (int)myCameraRotation.y;
        Debug.Log("Under 180 - Passing Value: " + tempVar[0]);
    }
    if (tempVar[0] > -90 && tempVar[0] <= 90)
    {
        tempVar[1] = (1 * Mathf.Abs(1 - (tempVar[0] / 90)));
    } // Calculate Z
    else if (tempVar[0] <= -90 && tempVar[0] > 90)
    {
        tempVar[1] = ((0 - 1) * Mathf.Abs(1 - (tempVar[0] / 90)));
    } // Calculate Z
    if (tempVar[0] > 0 && tempVar[0] <= 180)
    {
        tempVar[2] = (1 * Mathf.Abs(1 - (tempVar[0] / 90) / 2));
    } // Calculate X
    else if (tempVar[0] <= 0 && tempVar[0] > 180)
    {
        tempVar[2] = (-1 * Mathf.Abs(1 - (tempVar[0] / 90) / 2));
    } // Calculate X
    Debug.Log("Z Value : " + tempVar[1]);
    Debug.Log("X Value : " + tempVar[2]);

This is outputting some really funky values and I am not really sure what is going on with it, to be honest. As previously stated I am getting the Euler Angle of the myCamera GameObject, not the rotation.

The more I try to work on this the more horrific numbers start popping up, any help from someone who knows some math would be much appreciated.

1
Please keep your question as self-contained as possible. Off-site links, especially to "pastebin" type sites, are problematic for many people here. - tadman
Thank you for fixing it, when I had tried to do it in the code it gave me a red error flag and I could not post it. - Shane Powell

1 Answers

2
votes

If the y-rotation is the only rotation of the object, you can use the first column of the object's matrix as the forward direction.

If there are more transforms, you can calculate the direction you want with:

x = Math.Cos(myCameraRotation.y);
z = Math.Sin(myCameraRotation.y);