Search Unity

2D Rotation Issue

Discussion in '2D' started by ToastyCactus, Dec 8, 2013.

  1. ToastyCactus

    ToastyCactus

    Joined:
    Dec 8, 2013
    Posts:
    3
    Hello everyone, would first like to say that this is my first post here, so I'm sorry if this is in the wrong section. I'm currently attempting to make my first project, a simple 2D top down shooter, but have run into some problems with the rotation of my player sprite. I am currently trying to make my player point toward the mouse position, but only on the z plane. The problem that I have run into is my sprite rotates on the x and y plane as well, causing the sprite to tilt and get distorted.

    Basically when the mouse cursor is closer to the sprite, it looks fine, but the further the cursor gets away from the player, the player will begin to tilt toward the mouse instead of just turning on the z axis. I want to isolate the rotation to only the z axis so the sprite always stays flat, or at 0x and 0y. I've tried doing this multiple ways, with transform functions to quaternions, but it all ends up giving the same result.

    At the moment, I'm using three game objects, the camera, the player, and the crosshair. I have a script on the player that controls basic movement, and a script on the crosshair that basically just sets the crosshair sprite position to the mouse cursor's position. Then I have some code in my player script that tells it to LookAt the crosshair object. All code I'm using is in C#.

    The code I'm using for the crosshair:

    Code (csharp):
    1.  
    2.  
    3. Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1);
    4. Vector3 crosshair = Camera.main.ScreenToWorldPoint(mousePos);
    5.  
    6. transform.Translate (crosshair - transform.position);
    7.  
    8.  
    Then the code for the player:

    Code (csharp):
    1.  
    2. public Transform rotationTarget;
    3.  
    4. transform.LookAt(rotationTarget, Vector3.back);
    5.  
    I then set the rotationTarget to the crosshair. This almost gives me the effect that I want, but as I said before, the sprite is still tilting on the x and y axes the further away from the center of the screen I get. I need x and y to stay at 0 so the sprite never tilts I just have no idea how to go about doing this.
     
  2. Togglesworlh

    Togglesworlh

    Joined:
    Nov 12, 2013
    Posts:
    40
    I was having the same problem, and couldn't for the live of me figure out why it was happening. I eventually gave up and just calculated the Euler angle I needed on my own.

    Code (csharp):
    1.  
    2.         Vector2 currentDirection = rigidbody2D.velocity.normalized;
    3.         float rotation = -Mathf.Atan2 (currentDirection.x, currentDirection.y)*180/Mathf.PI;
    4.         transform.localEulerAngles = new Vector3(0,0,rotation);
    5.  
    Current direction is pretty easily found in your case by (rotationTarget.position - transform.position).

    Hopefully someone else can chime in with how to make LookAt work properly, because life and code would be a lot simpler with that working as I want it to!
     
  3. ToastyCactus

    ToastyCactus

    Joined:
    Dec 8, 2013
    Posts:
    3
    Thank you very much! This works exactly how I wanted it to. I'll have to do more research on these functions before I understand them entirely, but this at least gets me something I can work with. Very much appreciated.
     
  4. ToastyCactus

    ToastyCactus

    Joined:
    Dec 8, 2013
    Posts:
    3
    Okay, so now I have run into a new issue. The code Toggles provided fixed my initial tilting problem, but now the movement of my character is relative to the crosshair, when I want it to be separate. For example, when I press W, or "up", I want my character to literally go up on the screen, but as it is right now, when I press W, I simply go forward toward the crosshair. So if the crosshair is below my character, and I press W for up, the character will actually move down. When I want to turn left or right with A and D, my character rotates in circles around the crosshair. I'm basically wanting my movement to be relative to world space, and not relative to the crosshair position.

    Edit: Found the solution, I had to add Space.World to my movement parameters. Now it works exactly as expected.
     
    Last edited: Dec 9, 2013