i have an moveing object with an collider but the collider seems to do nothing here's the script: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class carLights : MonoBehaviour { private Animator anim; public GameObject player; void Start() { anim = GetComponent<Animator>(); anim.SetInteger("AnimationStateThing", 0); } private void OnTriggerEnter2D(Collider2D collision) { if(collision.tag == "TunnelActivate") { anim.SetInteger("AnimationStateThing", 1); Debug.Log("Tunnel enter"); } if (collision.tag == "TunnelDeActivate") { anim.SetInteger("AnimationStateThing", 0); Debug.Log("Tunnel leave"); } } private void Update() { transform.position = player.transform.position; } }
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.
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.
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.