Amazing User
Amazing User

Reputation: 3563

OnCollisionEnter doesn't work correct in Unity3D

I try to make a copy of the impossible game: http://flukedude.com/theimpossiblegame/

Red square are move in the right direction and when player see blue square, he should jump. If he will collide with the left side of the square (which is another square "WallCollider"), the game is over. It works, but if player will jump on the upper left corner, the game will be over too. Can't understand, why.

void OnCollisionEnter(Collision collision)
{
    if (collision.transform.name == "WallCollider")
        Debug.Log ("You lose");
}

enter image description here

Red and blue squares and WallCollider have box colliders. Only red square have a rigidbody.

I move red like this:

public GameObject player;

    public float speed = 5.0f;
    public float jumpPower = 10.0f;

    void Start ()
    {
        player = (GameObject)this.gameObject;
    }

    void Update ()
    {
        player.transform.position +=
        player.transform.right * speed * Time.deltaTime;
    }

I'm using 3d physics in a 2d environment. All scripts is located on the red square (player).

Blue box have one box collider. But near it I have WallCollider (Cube too), which have a box collider too, but it's height a little bit lower.

Upvotes: 0

Views: 1105

Answers (1)

game development germ
game development germ

Reputation: 1334

First of all, colliders have penetration distance so by jumping on upper left corner player collider may penetrate it down to the wall collider. Try to reduce penetration distance in Edit/Project settings/Physics.

Also it will be helpful to decrease fixed timestep to improve accuracy of physics simulation (look for Edit/Project settings/Time).

Upvotes: 2

Related Questions