Search Unity

How do you glue one object to another?

Discussion in 'Scripting' started by polan31, Oct 28, 2018.

  1. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    have an object that hits the wall that rotates.

    I would like them to cling to each other after collision.

    What I have is causing the object to stick only at the touch point.

    How to change it?

    My script:

    Code (CSharp):
    1.  
    2.        using System.Collections;
    3.        using System.Collections.Generic;
    4.        using UnityEngine;
    5.  
    6.  
    7.       public class CollWithPlayer : MonoBehaviour {
    8.      private bool moving;
    9.      // Use this for initialization
    10.      private void OnCollisionEnter2D (Collision2D coll) {
    11.          if (coll.gameObject.tag == "Player") {
    12.              moving = true;
    13.              coll.collider.transform.SetParent (transform);
    14.          }
    15.            
    16.        
    17.      }
    18.      private void OnCollisionExit2D (Collision2D coll) {
    19.          if (coll.gameObject.tag == "Player") {
    20.              moving = false;
    21.              coll.collider.transform.SetParent (null);
    22.          }
    23.    
    24. }
    25. }

    It stick only in Touch point (like on video).

    https://gfycat.com/pl/gifs/detail/QueasyFlickeringFrog
     
  2. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    91
    You mean that both edges are perfectly parallel? If yes, setting the z axis rotation of the player to the z axis rotation of the wall in OnCollisionEnter2D should be enough.
     
  3. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Not work :/

    Can I use the script to unhide the freeze X in rigidbody2d?

    Because, as I turn the Player X rotation, it works great, but if I stick to the object I have to turn it off again to be able to jump.
     
  4. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    91