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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

I need some help with collision

Discussion in 'Getting Started' started by Sorgun, Apr 29, 2018.

  1. Sorgun

    Sorgun

    Joined:
    Apr 28, 2018
    Posts:
    2
    So I'm not really good with scripting, and what I mean by that is that I really can't script anything to save my life. But with that said I have a college course that requires me to script, so I'm left with little choice but to ask for help.

    So I'll cut to the chase: I'm having some massive issues with collision. I'm trying to have it where when objects touch each other both objects get destroyed in the process, but instead they totally ignore each other and pass through.

    I also have a boundary box that's supposed to also destroy objects once they touch it, but it doesn't do anything. My projectiles just pass right through it meaning if you fire enough projectiles the game is eventually going to start slowing down.

    This is getting extremely frustrating, and my development of my alpha has hit a very solid brick wall.

    Help would be appreciated.



    Here is the script I'm using for my enemy:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class DestroyByContact : MonoBehaviour {

    public GameObject explosion;


    private void OnTriggerEnter(Collider other)
    {

    if (other.tag == "Boundary")
    {
    return;
    }
    Instantiate(explosion, transform.position, transform.rotation);
    Destroy(other.gameObject);
    Destroy(gameObject);
    }
    }





    and here's the script that my boundary is using:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class DestroyByBoundary : MonoBehaviour {

    private void OnTriggerExit(Collider other)
    {
    Destroy(other.gameObject.transform.parent.gameObject);
    }

    }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is this code from the space shooter tutorial? Just curious, as it looks familiar.

    Make sure you've setup trigger colliders and at least 1 of the interacting game objects has a rigidbody.
    You can view the trigger/collision matrix here (scroll down a bit): https://docs.unity3d.com/Manual/CollidersOverview.html

    If this is for 2D, make sure you use OnTriggerEnter2D and the like.

    Another curious question, is if this is for school, don't they teach you how to write code there? :)

    If you post more code in the future, please take a moment to look at this page for how to do it nicely: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  3. Sorgun

    Sorgun

    Joined:
    Apr 28, 2018
    Posts:
    2
    Hey nice catch, it is in fact the tutorial, I wish I could say I understood what I was listening too, nada, and yeah it's 2D.
    Thanks for the quick reply, I'll check for trigger colliers.

    And to answer you question, yeah they teach code there, but I think the issue has more to do with me. I just can't grasp these concepts, they're beyond my ability to comprehend.

    I can work just as hard in a programming class as I do an any other, and where I would receive an A for my effort in other classes, I receive a D, or a C at best in programming. It just isn't my strength.

    But I do what I can, which usually just involves me bashing my head against a brick wall until the deadline and getting a bad grade for my efforts.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, well I did a quick search of the tutorial, and even though it looks 2D, the code doesn't use OnTrigger2D and such, so that's not an issue.
    Just try to make sure you have the colliders/rigidbodies setup properly.