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

[Solved] Unable to get collision triggers to work

Discussion in 'Scripting' started by Novidoux, Jul 14, 2016.

  1. Novidoux

    Novidoux

    Joined:
    May 19, 2016
    Posts:
    19
    I've been trying to find a solution to this but for some reason it refuses to work.

    I have two objects, one moving toward the other. The moving object has a Circle Collider 2D component attached with "Is trigger" active. The moving object also has a script containing a trigger function like this
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         Debug.Log("Collision");
    4.     }
    And as far as I understand this should work. Both objects also have rigidbodies2d as well, which seem to be a requirement for this to work. Yet the console shows nothing.

    The object is moved by calling the transform.Translate function like this
    Code (CSharp):
    1. transform.Translate(new Vector2(0, Speed));
    I'm gonna need some help to figure this out.
    Thanks in advance!
     
  2. zoran404

    zoran404

    Joined:
    Jan 11, 2015
    Posts:
    520
    Instead of moving the objects directly, via their transforms, apply force to them (or set their velocities directly) in the rigidbody.

    Moving objects directly will avoid the physics system.
     
  3. Novidoux

    Novidoux

    Joined:
    May 19, 2016
    Posts:
    19
    I tried using AddForce, however it gives the object an acceleration, unless there is a way of go around that, but it didn't trigger anyways. That's why I went back with transform.
     
  4. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    I believe you can use Rigidbody2D.MovePosition the same way you are using transform.Translate. If you want to use forces it will require you to set up drag and other physics related variables so acceleration seems instant. Is your physics layer collision matrix (Edit > Project Settings > Physics 2D) set up correctly?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Last edited: Jul 15, 2016
  6. Novidoux

    Novidoux

    Joined:
    May 19, 2016
    Posts:
    19
    Not sure how the layer collision matrix should look like but they are all checked. MovePosition only changed the object position instantly. What I need is a constant motion in a direction. I tried putting it next to the other object but that didn't do anything. Could be because it is entering before the update.

    I've tried both using kinematic and setting the gravity to 0, neither of them works. Regarding the depth, this was something that I also have been thinking about whether it is part of the issue. However, both objects have the same Z value and they both reside with the Default layer.
     
  7. AssembledVS

    AssembledVS

    Joined:
    Feb 23, 2014
    Posts:
    248
    I would break down this problem into smaller, testable steps. Right now we do not know whether it is the transform.Translate method that is causing it, or something related to the setup of your colliders, or something else.

    Try to simply place the objects one on top of another in the scene, don't create any movement, and run the game. If your script prints to console, then we know it is related to movement/transform.Translate, as that is the only variable that changed. If not, then we know it is something else.
     
  8. Novidoux

    Novidoux

    Joined:
    May 19, 2016
    Posts:
    19
    Ok I finally solved it.

    Apparently both objects require a Collider component, which I don't recall reading or hearing anything about. I thought it was enough if one object had a collider, which could then sense other objects (at least objects with a rigidbody component).

    Anyway, thanks for all the help!