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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question: 2D Rigidbodies Won't Collide

Discussion in '2D' started by chromiumuptobad, Dec 12, 2022.

  1. chromiumuptobad

    chromiumuptobad

    Joined:
    Dec 10, 2022
    Posts:
    3
    I am just getting started with Unity, making a simple game. I have two rigidbodies, a projectile and an object. I want the projectile to stop once it touches the object. However, it just clips through the object. I have tried changing the type of moving from transform + projectileSpeed to velocity + projectileSpeed but it still hasn't worked.

    Here is the code for the projectile:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class dart : MonoBehaviour
    {
    public int dartSpeed;
    public Rigidbody2D dartBody;
    // Start is called before the first frame update
    void Start()
    {
    dartBody.velocity = Vector2.right * dartSpeed;
    }
    // Update is called once per frame
    void Update()
    {
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
    Debug.Log("Dart Deleted!");
    Destroy(gameObject);
    }

    }

    and here is the code for the stationary object:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Pipe : MonoBehaviour
    {
    public float moveSpeed = 5;
    public LogicManager logic;
    // Start is called before the first frame update
    void Start()
    {
    logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<LogicManager>();
    }
    // Update is called once per frame
    void Update()
    {

    transform.position = transform.position + (Vector3.left * moveSpeed) * Time.deltaTime;
    if (transform.position.x < -15)
    {
    Debug.Log("Pipe Deleted!");
    Destroy(gameObject);
    }
    }
    }

    I have rigid bodies and colliders on both of them, with the object on the default layer and the projectile on layer 6. WHY WON'T THEY COLLIDE?!
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    437
    I copied your scripts, put them on two game objects. One hits the other and disappears. Everything works as you assumed, so the problem is not in the scripts, but in the components.

    It's best to create a Sample Scene, create two new game objects (e.g. based on Sprite -> Square), add Rigidbody2D, BoxCollider2D to them, and scripts at the end. Do not modify anything, just drag the Rigidbody2D into the Dart script, and set the bullet speed. This is the minimum to be met.
     
    chromiumuptobad likes this.
  3. chromiumuptobad

    chromiumuptobad

    Joined:
    Dec 10, 2022
    Posts:
    3
    Thank you, I'll try to find out what's wrong.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    You are bypassing the physics system by doing the above.

    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121