Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2D flipcode help

Discussion in '2D' started by kingbudo, Feb 17, 2015.

  1. kingbudo

    kingbudo

    Joined:
    Feb 15, 2015
    Posts:
    5
    Hi, I'm not the best programmer but I'm having some issues when my sprite is flipped. This is my code.

    void Update () {
    //rotation
    Vector3 mousePos = Input.mousePosition;
    mousePos.z = 1f;

    Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
    mousePos.x = mousePos.x - objectPos.x;
    mousePos.y = mousePos.y - objectPos.y;

    float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    angle = Mathf.Clamp (angle, -15.0f, 30.0f);


    transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));

    when character is facing right it works perfectly but when the character is flipped left that's when I run into issues. Is there some way i can flip this code so it works on the left side?

    edit: I got it working
     
    Last edited: Feb 19, 2015
  2. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    I think I remember issues with screen / world position of the mouse having to subtract the mouse.y from the screen height or something or the other :D
    try change the one line to this and see :
    Code (csharp):
    1.  
    2. mousePos.y = (Screen.height-mousePos.y) - objectPos.y;
    but you should really flip the sprite using localScale, its much simpler :
    Code (csharp):
    1.  
    2. public void Flip()
    3. {
    4.      // Multiply the x component of localScale by -1.
    5.      Vector3 playerScale = transform.localScale;
    6.      playerScale.x *= -1;
    7.      transform.localScale = playerScale;
    8.      moveSpeed = moveSpeed * -1;
    9.  }
     
  3. kingbudo

    kingbudo

    Joined:
    Feb 15, 2015
    Posts:
    5
    thanks but I have a separate script on the body that flips the sprites that uses that. My issue is the movement code for arm which i have rotating on the z axis using this code. i have it also locked between -15 and 30. but once the character is flipped the code no longer works. the arm is still moving like the arm is facing right. The sprite for the is flipped. I have a idea if there was some way to flip the coordinates for the x-axis when flipped. that would solve my problems but I have no idea how to do that.
     
    Last edited: Feb 18, 2015
  4. MaxHarper

    MaxHarper

    Joined:
    Apr 6, 2015
    Posts:
    1
    Code (CSharp):
    1.  mouse_pos = Input.mousePosition;
    2.         mouse_pos.z = 5.23f; //The distance between the camera and object
    3.         object_pos = Camera.main.WorldToScreenPoint(target.position);
    4.         mouse_pos.x = mouse_pos.x - object_pos.x;
    5.         mouse_pos.y = mouse_pos.y - object_pos.y;
    6.         if (Player.directionalInput.x >= 1 || bFlag)
    7.         {
    8.             transform.localPosition = position;
    9.             rend.flipX = false;
    10.             bFlag = true;
    11.             angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    12.             //Debug.Log(angle);
    13.             if (angle <= 30 && angle >= -16)
    14.             {
    15.                 transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    16.             }
    17.         }
    18.         if (Player.directionalInput.x <= -1 || !bFlag)
    19.         {
    20.             rend.flipX = true;
    21.             bFlag = false;
    22.  
    23.             transform.localPosition = NewPosition;
    24.             angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    25.             Debug.Log(angle+180);
    26.             angle += +180;
    27.             if (angle <= 16 && angle >= 0 || angle <= 360 && angle >= 330)
    28.             {
    29.             transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    30.             }
    31.         }