Set Object to new Transform

Hi.

I made a 2d game and wrote an script for climb the wall that work very well. in my game I have two wall and want with below script after RayCast collide to edge of each wall, set player (x,y) position to edge of wall. like below picture.

        Debug.DrawLine (ClimbWall_Start.position, ClimbWall_End.position, Color.blue);
        if (anim.GetCurrentAnimatorStateInfo (0).IsName ("LongJump") && IsGrounded == false)
        {
            if (Physics2D.Linecast (ClimbWall_Start.position, ClimbWall_End.position, 1 << LayerMask.NameToLayer ("Ground")))
            {
                if (GetWall == false)
                {
                    GetWall = true;
                    rb.gravityScale = 0f;
                    rb.velocity = Vector2.zero;
                    anim.Play ("GetWall");
                    moveleft = 0;
                    moveright = 0;
                    if (FaceIsRight == true)
                    {             
                        transform.position = new Vector2 (GameObject.FindGameObjectWithTag("Ground").transform.position.x - GetComponent<BoxCollider2D>().bounds.size.x, GameObject.FindGameObjectWithTag("Ground").transform.position.y + 0.1f);
                    }
                    else if (FaceIsLeft == true)
                    {
                        transform.position = new Vector2 (GameObject.FindGameObjectWithTag("Ground").transform.position.x + GetComponent<SpriteRenderer>().bounds.size.x , GameObject.FindGameObjectWithTag("Ground").transform.position.y + 0.1f);
                    }
                }
            }
        }

but problem is when I cross the first wall, when I try to get second wall, player move to first wall and I can’t cross from second wall.

if possible please tell me where is the problem.

Thanks.

no image (which you would have seen when you submitted the post but hey… ) so its not clear what you are after.

2d raycasts work differently to 3d raycasts. Look at the API for each, 3d raycasts return a bool and you get the raycasthit from an “out” parameter. 2d raycasts return the ratcasthit.

I suspect the issue you are having is that you are working off the ground object position which is going to give you the same position each time since it doesn’t change, rather than working with the actual hit location from the linecast.

Sorry, I Fixed Picture.

Thanks, I fixed Problem with use RaycastHit2D.:slight_smile: