c# - 2D 碰撞检测不起作用

标签 c# unity-game-engine collision detection

我一直在尝试让碰撞发挥作用,但到目前为止效果不佳。

Screenshot Unity

这是我放在球对象上的 moveBall.cs。

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);
    }
  }
}

这是我放在 Player 对象上的 movePlayer.cs。

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);
    }
  }
}

如果有人能给我一个提示/解决方案,那将对我有很大帮助!

最佳答案

我立即发现的一个问题是您使用的是 RigidBody 组件而不是 RigidBody2D 组件。您需要小心使用哪些组件。

此外,OnTriggerEnter2D()OnCollisionEnter2D()是您正在寻找的内容,而不是 OnCollisionTrigger2D。

如果选择使用 3d 组件,请查看 OnCollisionEnter()OnTriggerEnter()

关于c# - 2D 碰撞检测不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27867712/

相关文章:

c# - LINQ 连接两个表

c# - 如何使用 UDP 广播进行网络发现

unity-game-engine - 访问被拒绝 GVR SDK

android - java.lang.SecurityExcption : WifiService: Neither usr 10140 nor curent proces hs andrid. prmission.CHANGE_WIFI_STATE

javascript - 为 Phaser 创建碰撞形状

c# - 在 C# 中打印 (wpf)

c# - 循环遍历 MySql reader 中的每个列名称和值?

unity-game-engine - Unity 计算着色器同步

javascript - p5.j​​s 中的碰撞处理

java - Java 中的吃 bean 碰撞