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

Object colliding with nothing.

Discussion in '2D' started by Tibor_coder, Oct 23, 2020.

  1. Tibor_coder

    Tibor_coder

    Joined:
    Mar 27, 2020
    Posts:
    25
    I am making a stickman parkour/plat former type game. I want my player to be able to throw objects. However, when I throw the object it sometimes gets stuck in mid air. It behaves like there is a collider below it, however there is none. I set the RigidBody2D colliion detection to continous. No other object is under it. Here is a picture:

    Here are the settings on the box/crate in the photo:


    Any ideas?
     

    Attached Files:

  2. ZygoUgo

    ZygoUgo

    Joined:
    Jul 11, 2017
    Posts:
    63
    Do you have your elements separated by layer correctly? That would be my first guess.
     
  3. Tibor_coder

    Tibor_coder

    Joined:
    Mar 27, 2020
    Posts:
    25
    Yes they are. Why is that a problem?
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    What is the code you are using for throwing the box?
     
  5. Tibor_coder

    Tibor_coder

    Joined:
    Mar 27, 2020
    Posts:
    25
    Here is the code:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class HoldObjectScript : MonoBehaviour
    7. {
    8.     // Arm that will hold object
    9.     public Rigidbody2D arm;
    10.  
    11.     // Player root object
    12.     public GameObject root;
    13.  
    14.     // Point where to connect the object
    15.     public Transform connectPoint;
    16.  
    17.     // Joint used to connect objects
    18.     private HingeJoint2D currentJoint;
    19.  
    20.     // Settings
    21.     public float throwStrength;
    22.  
    23.     public void TryHoldObject(GameObject obj)
    24.     {
    25.         if (Input.GetKeyDown(KeyCode.H))
    26.         {
    27.             // We get the joint that will be used to connect the object
    28.             HingeJoint2D joint = obj.GetComponent<HingeJoint2D>();
    29.  
    30.             // We connect the object to arm
    31.             joint.connectedBody = arm;
    32.  
    33.             joint.connectedAnchor = connectPoint.localPosition;
    34.  
    35.             // We disable all collisions between object and player
    36.             DisableCollisions(obj.GetComponent<Collider2D>());
    37.  
    38.             // Enable the joint
    39.             joint.enabled = true;
    40.            
    41.             // Remember the joint
    42.             currentJoint = joint;
    43.         }
    44.     }
    45.  
    46.     public void StopHolding()
    47.     {
    48.         EnableCollisions();
    49.         currentJoint.enabled = false;
    50.     }
    51.  
    52.     void DisableCollisions(Collider2D collider)
    53.     {
    54.         Collider2D[] colliders = root.GetComponentsInChildren<Collider2D>();
    55.  
    56.         for (int i = 0; i < colliders.Length; i++)
    57.         {
    58.             Physics2D.IgnoreCollision(colliders[i], collider);
    59.         }
    60.     }
    61.  
    62.     void EnableCollisions()
    63.     {
    64.         Collider2D[] colliders = root.GetComponentsInChildren<Collider2D>();
    65.         Collider2D collider = currentJoint.GetComponentInParent<Collider2D>();
    66.  
    67.         for (int i = 0; i < colliders.Length; i++)
    68.         {
    69.             Physics2D.IgnoreCollision(colliders[i], collider, false);
    70.         }
    71.     }
    72.  
    73.     public void ThrowObject(Vector2 direction)
    74.     {
    75.         Rigidbody2D objectRb = currentJoint.GetComponentInParent<Rigidbody2D>();
    76.  
    77.         StopHolding();
    78.  
    79.         objectRb.velocity = direction * throwStrength;
    80.     }
    81. }
    82.  
    I basically receive a direction and apply a force to it.