Search Unity

collider does not move with object

Discussion in '2D' started by FireJojoBoy, Jan 19, 2020.

  1. FireJojoBoy

    FireJojoBoy

    Joined:
    Oct 30, 2019
    Posts:
    65
    i have an moveing object with an collider but the collider seems to do nothing
    here's the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class carLights : MonoBehaviour
    6. {
    7.  
    8.     private Animator anim;
    9.  
    10.     public GameObject player;
    11.  
    12.     void Start()
    13.     {
    14.         anim = GetComponent<Animator>();
    15.         anim.SetInteger("AnimationStateThing", 0);
    16.     }
    17.  
    18.     private void OnTriggerEnter2D(Collider2D collision)
    19.     {
    20.  
    21.         if(collision.tag == "TunnelActivate")
    22.         {
    23.             anim.SetInteger("AnimationStateThing", 1);
    24.             Debug.Log("Tunnel enter");
    25.         }
    26.  
    27.         if (collision.tag == "TunnelDeActivate")
    28.         {
    29.             anim.SetInteger("AnimationStateThing", 0);
    30.             Debug.Log("Tunnel leave");
    31.         }
    32.     }
    33.  
    34.  
    35.     private void Update()
    36.     {
    37.         transform.position = player.transform.position;
    38.     }
    39. }
     
  2. Nyxal_Indie

    Nyxal_Indie

    Joined:
    Jun 26, 2019
    Posts:
    179
    Is one of the collider components setted to trigger?
     
  3. Wezai

    Wezai

    Joined:
    Dec 30, 2016
    Posts:
    74
    What do you mean by nothing, is it not registering at all or can you at least see these debug logs being called?

    Does your car have a rigidbody? When two objects collide at least one of them needs a rigidbody otherwise OnTriggerEnter2D will do nothing as far as I know.

    If you are not using physics just set the rigidbody to kinematic so it can still detect collisions.
     
  4. FireJojoBoy

    FireJojoBoy

    Joined:
    Oct 30, 2019
    Posts:
    65
    [Q
    nevermind i fixed it by attaching a Ridgidbody
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    I wanted to highlight in-case you didn't know but you should never change the Transform on a GameObject that has physics components on it. A collider without a Rigidbody2D is a static collider (not moving) and moving it is expensive. When you add a Rigidbody2D, you're giving it control over the Transform so changing the Transform is stomping over what it's doing.

    This is what Rigidbody2D.MovePosition and Rigidbody2D.MoveRotation is for. Note that these will only update per fixed-update and not per-frame unless you're running your physics per-frame. This is what interpolation is for on the Rigidbody2D, to interpolate for smooth motion when doing physics at a fixed update interval.
     
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    If your collier is a component on the Player, you don't need to move it explicitly, it will move with the player...unless I'm missing something.

    You also might use
    void OnTriggerEnter2D(Collider2D collider) // instead of naming the return value "collision"
    Both will work, but the names are confusing, because Trigger returns just the Collider, and Collision returns a Collision, which has more info in it (and not needed if all you want is the collider). That said, triggers allow rigidbodies to pass through the collider, which is sometimes what you want, like passing through an invisible zone or a ghost or something.

    Functions without "private" or "public" are private by default, so no need for the "private" keyword on Update, OnTriggerEnter2D, etc., unless you want it for clarity. Personally I'd say just know that everything is private unless specified otherwise.