tutuum
tutuum

Reputation: 15

Unity3D Collider2D don't collide with translated Collider2D

I am trying to bounce the tile above the circle on top of the platform, there is no problem when the platform is fixed, but when the script who make the platform move is activated, the tile crash the circle.

sketch describing the problem

Inspector settings

This is the script for moving the platform:

using UnityEngine;
using System.Collections;

public class PlatformMoveH : MonoBehaviour {

    public float min;
    public float max;
    public float speed;
    public float sens;

    void Start(){
        sens *= speed;
    }

    void Update () {
        if (transform.localPosition.x > max) {
            sens = -speed;
        } else if (transform.localPosition.x < min){
            sens = speed;
        }
        transform.Translate (sens * Time.deltaTime, 0, 0);
    }
}

i can't add pictures here but in unity answers you can understand the problem better with pictures

http://answers.unity3d.com/questions/1009619/collider2d-and-translate-problem.html

i have no idea what is the problem.
can anyone help me please

Upvotes: 1

Views: 100

Answers (1)

Stefan Hoffmann
Stefan Hoffmann

Reputation: 3234

You have to add a kinematic Rigidbody2D to your moving colliders. Colliders without a rigidbody are treated as static colliders by Unity and should never be moved.

From the official documentation:

The physics engine assumes that static colliders never move or change and can make useful optimizations based on this assumption. Consequently, static colliders should not be disabled/enabled, moved or scaled during gameplay. If you do change a static collider then this will result in extra internal recomputation by the physics engine which causes a major drop in performance. Worse still, the changes can sometimes leave the collider in an undefined state that produces erroneous physics calculations. For example a raycast against an altered Static Collider could fail to detect it, or detect it at a random position in space.

Upvotes: 2

Related Questions