Search Unity

Setting anchor of Distance Joint to contact point

Discussion in 'Physics' started by codejoy, Jun 15, 2020.

  1. codejoy

    codejoy

    Joined:
    Aug 17, 2012
    Posts:
    204
    If I am in the editor of my game, I see the rectangle that is my 'grapple surface'. It is pretty simple rectangle, and when I move the anchor point in game around and see it during non-maximized play. I can see that -.15 is the left corner and .15 is the right corner according to the anchor (where it moves when I set the anchor X to those values).

    Okay, so I have this little arrow I shoot and if it hits the rectangle, I run this:


    Code (CSharp):
    1.  void OnCollisionEnter2D(Collision2D col)
    2.     {
    3.         Collider2D collider = col.collider;
    4.      
    5.         if (col.gameObject.tag == "PlayerProjectile")
    6.         {
    7.             Arrow arrow = col.gameObject.GetComponent<Arrow>();
    8.  
    9.          
    10.             if (arrow.GetArrowType() == Arrow.ShotType.Grappling)
    11.             {
    12.  
    13.                 //get WEHRE collition happened
    14.                 ContactPoint2D contactPoint = col.contacts[0];
    15.                 Vector2 newPoint = cam.InverseTransformPoint(contactPoint.point); //set it to local spacE?
    16.  
    17.                 newPoint.y = 0; //don't care about the Y value
    18.                 dj.anchor = newPoint;
    19.                 //create a distance joint here and set the player to the 2d
    20.                 dj.connectedBody = thePlayer.GetComponent<Rigidbody2D>();
    21.             }
    22.         }
    23.     }

    Alas when I run the game and fire the arrow, the anchor X point is something like:
    .87869 which is way to the far right of my rectangle (Remember the far right is .15 when I move the anchor around).

    That inverseTransformPoint gets my point closer (0.8786...) before without it the point was like 26 something (way off the screen). But I must be missing a step since I am looking for a number of about -.15 (this is the value the Anchor X is when it is in the left point of the rectangle. If I shoot at that left point , the contact point after the transform says .87869 not -.15. So not sure what step I am missing?? (The attached pic is how it 'should' look, this is me readjusting the X coordinate of the anchor to -.15 after I shot the arrow, which yields a anchor x point of 0.87blahblah with the above code.
     

    Attached Files: