GameObject with multiple collider2D,
In my game there are obstacles falling from the top of the screen at different speeds. Obstacles have physics and i'd like to have them interact with eachother (obstacles pushing other obstacles). The problem is that with the collider2D set to Is Trigger, they ignore physics. If i try to ad a second collider2D (of the same type) that isn't set to Is Trigger i can get them to interact but they also interact with the physics of my player (the obstacle is supposed to despawn on collision with player, and it does, but it also move my player physically). I've searched solution in parenting but it is not clear to me how to fix this problem. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Ferro : MonoBehaviour
{
public float randomSpeedFactor;
public int resources; //Risorse date al player
private float despawnY = -11; //Limite Y-
private ResourceController resourceController; //riferimento alla classe ResourceController
private SpeedController speedController;
private void Awake()
{
resourceController = GameObject.FindObjectOfType<ResourceController>();
speedController = GameObject.FindObjectOfType<SpeedController>();
}
private void Start()
{
randomSpeedFactor = Random.Range(-3, 3);
}
private void Update()
{
transform.Translate(Vector2.down * (speedController.speed + randomSpeedFactor) * Time.deltaTime);
if (transform.position.y <= despawnY)
{
Destroy(gameObject);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
Debug.Log("Hai raccolto del Ferro");
resourceController.Ferro += resources;
Destroy(gameObject);
}
}
}
,
Answer by lgarczyn · Dec 03, 2019 at 01:56 AM
Don't use Translate to move non-kinematic RigidBodies, just use velocity. Do not set them to IsTrigger if you want them to collide with anything, and use OnCollisionEnter if they are not trigger instead of OnTriggerEnter. If you want them to ignore the player use Layers or IgnoreCollision.