1
\$\begingroup\$

So I'm making a game in Unity. After many failed attemps from either being bored of the idea, or something going wrong and me giving up I think (hope) that will be the good one.

I have made a simple racing game with a track and a car. I am using transform.Translate to move it, but when I use transform.Rotate to turn it goes around the x axis. How do I change the axis it goes around? I cant find anything online.

            if (Input.GetAxisRaw("Horizontal") > 0.5f)
    {
        transform.Rotate(Vector3.right * Time.deltaTime * handling);
    }
    if (Input.GetAxisRaw("Horizontal") < -0.5f)
    {
        transform.Rotate(Vector3.left * Time.deltaTime * handling);
    }
    if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
    {
        transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
    }

Thanks for any help you can give me.

\$\endgroup\$
5
\$\begingroup\$

The parameter you pass to transform.Rotate is a a vector with 3 elements. The 3 elements say how far to rotate around the x-axis, y-axis and z-axis. Vector3.right and Vector3.left are shorthands for Vector3(1.0f, 0.0f, 0.0f) and Vector3(-1.0f, 0.0f, 0.0f). These are rotations around the x-axis.

When you want to turn an object around the z-axis, for example, you would use:

transform.Rotate(new Vector3(0.0f, 0.0f, 1.0f) * handling * Time.deltaTime);

which can be abbreviated as

transform.Rotate(Vector3.forward * handling * Time.deltaTime);

When you use vectors for rotation instead of translation, the names of the constants defined in Vector3 are a bit misleading. So I would recommend you to either write out all three components as in my first example, or define your own constants like this:

static class RotationVector {
    static readonly Vector3 rollLeft = Vector3.forward;
    static readonly Vector3 rollRight = Vector3.backward;
    static readonly Vector3 yawRight = Vector3.up;
    static readonly Vector3 yawLeft = Vector3.down;
    static readonly Vector3 pitchDown = Vector3.left;
    static readonly Vector3 pitchUp = Vector3.right;
}

Note: these assignments assume that in your game x-axis means "forward" and the y-axis means "up" (like the Vector3 constants assume). They are different depending on which axis represents which direction in your game (in 2d-games seen from the bird-eye perspective, many people use y as the north-south axis and z as the up-down axis).

\$\endgroup\$
1
\$\begingroup\$

I worked it out finally. I use forward and back as the rotation direction. I was literally just rolling through vector options to find it though. Pure luck.

\$\endgroup\$
  • 1
    \$\begingroup\$ When you want to gain a deeper understanding of Unity you need to know not just how it works but rather why it works. I hope my answer is able to provide you with more insight. \$\endgroup\$ – Philipp Nov 28 '15 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.