1
\$\begingroup\$

first time posting here. What I want to do seems extremely simple, but I'm not familiar enough with Unity to be able to implement it. Also, I wasn't able to find an answer that exactly solved my problem, but I'm sure it's simple enough.

I'm trying to rotate a sprite based on left arrow down and right arrow down:

    if (Input.GetKey (KeyCode.RightArrow)) {
        this.transform.Rotate (new Vector3(0, 0, -2));
    }

    if (Input.GetKey (KeyCode.LeftArrow)) {
        this.transform.Rotate (new Vector3(0, 0, 2));
    }

Once the sprite has been rotated, I would like to be able to apply a force in the direction it's facing by holding down the up arrow. And when the down arrow is held down, apply a negative force (or the same force in the opposite direction).

Here's what I have thus far:

    if (Input.GetKeyDown (KeyCode.UpArrow)) {
        Vector2 direction = new Vector2 (Mathf.Cos(body.rotation), Mathf.Sin(body.rotation));
        body.AddForce (direction * 10);
    }

This code just moves it along some unchanging angle that body (Ridigbody2D) has. Again, this is probably an extremely simple issue, but I can't seem to find anything that helps. I know I'll probably feel like an idiot when I see the solution!

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

Here are two extension methods that will help you:

/// <summary> 
/// Returns the positive angle in degrees of given Vector2. This method assumes +X axis is 0 degrees. 
/// </summary>
public static float Rotation(this Vector2 vector2)
{
    return Mathf.Atan2(vector2.y, vector2.x) * Mathf.Rad2Deg;
}

/// <summary> 
/// Rotates the Vector2 by given angle. 
/// </summary>
public static Vector2 Rotate(this Vector2 v, float degrees)
{
    float sin = Mathf.Sin(degrees * Mathf.Deg2Rad);
    float cos = Mathf.Cos(degrees * Mathf.Deg2Rad);

    float tx = v.x;
    float ty = v.y;
    v.x = (cos * tx) - (sin * ty);
    v.y = (sin * tx) + (cos * ty);
    return v;
}

What you need to take care of is, Rigidbody2D assumes +Y axis is 0 degrees, but our method assumes +X axis is 0 degrees; so you have to extract 90 degrees from the result of Rotation() method before applying it to Rigidbody2D's rotation.

This is not a case for Rotate() method, you can use it directly since it is rotating relative to the vector itself.

\$\endgroup\$
0
\$\begingroup\$

Firstly, if you want the player to hold the up arrow you should use Input.GetKey() instead of Input.GetKeyDown(). Then you can create an empty object and then make the sprite it's child and rotate it so that when ever this empty object is facing forward the sprite is visible. Then you just move the empty object forward by simply applying the force forward like this: GetComponent<Rigidbody2D>().AddForce(transform.forward);. I hope this helps, sorry i couldn't test it yet, but I'm quite sure it will work.

\$\endgroup\$

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.