Carrier
Carrier

Reputation: 89

2D Collision detection not working

I've been trying to get collision to work but so far no good.

Screenshot Unity

This is my moveBall.cs which I put on my Ball Object.

using UnityEngine;
using System.Collections;

public class moveBall : MonoBehaviour {

float balSnelheid = 1;

void Update () {
    transform.Translate (0, balSnelheid * Time.deltaTime, 0);
}

void OnCollisionTrigger2D (Collision2D coll) {
    if (coll.gameObject.name == "Brick") {
        Destroy(coll.gameObject);
    }
  }
}

And this is my movePlayer.cs which I put on my Player Object.

using UnityEngine;
using System.Collections;

public class movePlayer : MonoBehaviour {

public float snelheid;

// Use this for initialization
void Start () {
    Screen.showCursor = false;
}

// Update is called once per frame
void Update () {
    if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime < 2.9) {
        if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime > -2.9) {
            transform.Translate(Input.GetAxis("hor") * snelheid * Time.deltaTime, 0, 0);
        }
    }

    if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime > 3) {
        transform.position = new Vector3(2.9f, -4.7f, 0);
    } else if (transform.position.x + Input.GetAxis ("hor") * snelheid * Time.deltaTime < -3) {
        transform.position = new Vector3(-2.9f, -4.7f, 0);
    }
  }
}

If anyone could give me a tip/solution it would help me out a lot!

Upvotes: 2

Views: 170

Answers (1)

Scott
Scott

Reputation: 93

One issue I see right off the bat is that you are using the RigidBody component rather than the RigidBody2D component. You need to be careful which components you use.

Also, OnTriggerEnter2D() or OnCollisionEnter2D() is what you are looking for, not OnCollisionTrigger2D.

If using the the 3d components was by choice, please look at OnCollisionEnter() or OnTriggerEnter()

Upvotes: 2

Related Questions