简体   繁体   English

如何检查物体碰撞

[英]How to check objects collisions

I want that 3 objects(boxColliders)are checked for collisions with other Cubes.If collision is TRUE the object script must inform the Player.How to move between two scripts? 我希望检查3个对象(boxColliders)是否与其他多维数据集发生冲突。如果冲突为TRUE,则对象脚本必须通知Player。如何在两个脚本之间移动? If the objects collides with cubes ,the player can't move to this direction. 如果物体与立方体碰撞,玩家将无法朝这个方向移动。

Image 图片

I tried something like this but script checked only the player. 我尝试过类似的操作,但脚本仅检查播放器。

void OnCollisionStay(Collision collisionInfo){
      if(collisionInfo.gameObject.tag == "Finish")
    {

        Debug.Log ("collision");
    }

}

This is full player code 这是完整的播放器代码

private Vector3 offset;

public GameObject player;
public GameObject center;

public GameObject right;
public GameObject left;


public int step=9;

public float speed =(float) 0.01;

bool input=true;

void Start () {

}

void Update () {


    if(input==true)
    {

        if (Input.GetKey (KeyCode.RightArrow)) {

            StartCoroutine ("moveRight");
            input = false;

        }
        if (Input.GetKey (KeyCode.LeftArrow)) {
            StartCoroutine ("moveLeft");
            input = false;
        }

    }

}


 IEnumerator moveLeft(){

    for(int i=0;i<(90/step);i++){

        player.transform.RotateAround (left.transform.position, Vector3.forward, step);
        yield return new WaitForSeconds (speed);
    }
    center.transform.position = player.transform.position;
    input = true;

}

 IEnumerator moveRight(){

    for(int i=0;i<  (90/step);i++){
        player.transform.RotateAround (right.transform.position, Vector3.back, step);
        yield return new WaitForSeconds (speed);
    }
    center.transform.position = player.transform.position;
    input = true;

}

void OnCollisionStay(Collision collisionInfo){
    if(collisionInfo.gameObject.tag == "Finish")
    {

        Debug.Log ("collision");
    }

}

Put colliders and collision events on all the GameObjects that you want to recognize collisions for. 将碰撞器和碰撞事件放在要识别碰撞的所有GameObject上。 If you just want to access the script of the other objects and you aren't sure which object you're colliding with you can do something like this: 如果您只想访问其他对象的脚本,但是不确定要与哪个对象发生冲突,则可以执行以下操作:

void OnCollisionStay(Collision collisionInfo)
{
    var cubeScript = collisionInfo.gameObject.GetComponent<CubeScriptTypeHere>();        
    var playerScript = collisionInfo.gameObject.GetComponent<PlayerScriptTypeHere>();

    if(cubeScript != null)
    {
        //This object hit a cube
        //Do something with cubeScript
    }
    if(playerScript != null)
    {
        //This object hit a player
       //Do something with playerScript
    }
}

Obviously replace "ScriptTypeHere" with whatever you named the actual scripts. 显然,将“ ScriptTypeHere”替换为您命名的实际脚本。 Then you can access all of the public methods, properties, etc. of external scripts from your collision event. 然后,您可以从碰撞事件访问外部脚本的所有公共方法,属性等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM