Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D collision is way off when flipped (Puppet2D)

Discussion in '2D' started by Runetass, Nov 17, 2014.

  1. Runetass

    Runetass

    Joined:
    Jun 4, 2013
    Posts:
    16
    Hey.
    This issue is kind of hard to track down, because everything looks fine, it just doesn't behave as expected.
    Made a video to demonstrate my issue.
    Summary: Lots of unpredictable but consistently *off* collision detection when character faces the other way, although all colliders are aligned perfectly.

    Code included in spoiler below. Any idea what might be going on?



    Warning: lots of incomplete functions, but shouldn't be related to this issue.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.     Animator anim;
    7.     public float speed = 6f;
    8.     public float runMultiplier = 4;
    9.     float speedMultiplier = 1;
    10.     Puppet2D_GlobalControl globalControl;
    11.     public Transform head;
    12.     public Transform hip;
    13.     public Transform Lhand;
    14.     public Transform Rhand;
    15.     float headRota = 0;
    16.     float crouchRota = 0;
    17.     float mouseX = 0; float mouseY = 0;
    18.    
    19.    
    20.     void Start ()
    21.     {
    22.         anim = GetComponent<Animator> ();
    23.         globalControl = GetComponent<Puppet2D_GlobalControl> ();
    24.     }
    25.  
    26.     void FixedUpdate ()
    27.     {
    28.         float move = Input.GetAxis ("Horizontal");
    29.  
    30.         headRotation();
    31.        
    32.  
    33.         onRun ();
    34.         OnTurnAround (move);
    35.         Move (move);
    36.         OnCrouch ();
    37.         onMoveHands();
    38.     }
    39.  
    40.     void LateUpdate()                    //LateUpdate can be used to override animation data, per frame.
    41.     {
    42.         //Crouch-mechanics
    43.         hip.localEulerAngles = new Vector3(0, 0, crouchRota);
    44.  
    45.         //Hand stretch
    46.         Lhand.transform.Translate(new Vector2(mouseY, mouseX));  
    47.  
    48.         //Rotate head with mouse
    49.         head.localEulerAngles = new Vector3 (0, 0, headRota);
    50.     }
    51.  
    52.     void onMoveHands()
    53.     {
    54.         float retract = 0.2f;
    55.         if (Input.GetAxis("HandStretch") > 0)            // Mouse1 (Unity: mouse 0)
    56.         {
    57.             mouseX += (Input.GetAxis("Mouse X"));
    58.             mouseY -= (Input.GetAxis("Mouse Y"));
    59.         }
    60.         else if (mouseX > 0)
    61.         {
    62.             mouseX -= retract;
    63.         }
    64.         else if (mouseY < 0)
    65.             mouseY += retract;
    66.     }
    67.  
    68.     void headRotation()
    69.     {
    70.         if (anim.GetBool("Crouching") == false && Input.GetAxis("HandStretch") == 0)
    71.             headRota -= Input.GetAxis("Mouse Y") * 2;
    72.     }
    73.  
    74.     //Running
    75.     void onRun() {
    76.         if (Input.GetKey (KeyCode.LeftShift))
    77.             speedMultiplier = runMultiplier;
    78.         else
    79.             speedMultiplier = 1;
    80.     }
    81.  
    82.     //Crouching
    83.     void OnCrouch()  
    84.     {
    85.         if (Input.GetKey (KeyCode.C)) {                        //On C-press
    86.             anim.SetBool ("Crouching", true);                //Play the animation
    87.             crouchRota += Input.GetAxis("Mouse Y") * 2.5f;
    88.             headRota = 0;
    89.         }
    90.         else {                                                //On releasing C
    91.             anim.SetBool ("Crouching", false);                //Stop animation
    92.             if (crouchRota < 0)
    93.                 crouchRota += 6;
    94.         }
    95.     }
    96.  
    97.     //Turning
    98.     void OnTurnAround(float direction)
    99.     {
    100.         if (direction > 0 && globalControl.flip == true)
    101.             globalControl.flip = false;
    102.         else if (direction < 0 && globalControl.flip == false)
    103.             globalControl.flip = true;
    104.     }
    105.  
    106.     //Makes you move
    107.     void Move(float move)
    108.     {
    109.         rigidbody2D.velocity = new Vector2 (move * speed * speedMultiplier, rigidbody2D.velocity.y);
    110.         anim.SetFloat ("moveSpeed", Mathf.Abs (Input.GetAxis ("Horizontal")) * speedMultiplier);
    111.     }
    112. }
     
  2. jamieniman

    jamieniman

    Joined:
    Jan 7, 2013
    Posts:
    985
    Unity had a bug where 2d colliders didn't update properly - it should be fixed if you get Unity version 4.5.5 patch 2
     
    TheWarper likes this.
  3. Runetass

    Runetass

    Joined:
    Jun 4, 2013
    Posts:
    16
    Thanks!
    Patch 2 fixed it indeed. Great.