【问题标题】:I am developing a Merge kind game. But Collide Problem?我正在开发一个合并类游戏。但是碰撞问题?
【发布时间】:2021-09-23 09:15:41
【问题描述】:

我正在开发一个合并类游戏。当这两个对象发生碰撞时,必须生成一个对象。但是在我的代码中,我得到了两个对象。您对这种情况有任何想法,或者您有合并游戏的算法吗?谢谢。

`使用 System.Collections; 使用 System.Collections.Generic; 使用 UnityEngine;

公共类合并:MonoBehaviour {

public GameObject Circle;
public GameObject Heptagon;
public GameObject Hexagon;
public GameObject Octagon;
public GameObject Pentagon;
public GameObject Square;
public GameObject Triangle;


void start()
{
    
}

void update()
{
   
}

public void OnCollisionEnter2D(Collision2D collision)
{

    if (collision.gameObject.tag == "Triangle")
    {
        Debug.Log("Merged !!!");
        Instantiate(Circle, new Vector2(Triangle.transform.position.x, transform.position.y), Quaternion.identity);
        Destroy(Triangle);
    }

    else if (collision.gameObject.tag == "Circle")
    {
        Debug.Log("Merged !!!");
        Instantiate(Square, new Vector2(Circle.transform.position.x, Circle.transform.position.y), Quaternion.identity);
        Destroy(Circle);


    }


    else if (collision.gameObject.tag == "Square")
    {
        Debug.Log("Merged !!!");
        Instantiate(Pentagon, new Vector2(Square.transform.position.x, Square.transform.position.y), Quaternion.identity);
        Destroy(Square);


    }

    else if (collision.gameObject.tag == "Pentagon")
    {
        Debug.Log("Merged !!!");
        Instantiate(Hexagon, new Vector2(Pentagon.transform.position.x, Pentagon.transform.position.y), Quaternion.identity);
        Destroy(Pentagon);


    }

    else if (collision.gameObject.tag == "Hexagon")
    {
        Debug.Log("Merged !!!");
        Instantiate(Heptagon, new Vector2(Hexagon.transform.position.x, Hexagon.transform.position.y), Quaternion.identity);
        Destroy(Hexagon);


    }


    else if (collision.gameObject.tag == "Heptagon")
    {
        Debug.Log("Merged !!!");
        Instantiate(Octagon, new Vector2(Heptagon.transform.position.x, Heptagon.transform.position.y), Quaternion.identity);
        Destroy(Heptagon);
    }


}

} `

【问题讨论】:

  • 因为 2 个具有相同脚本的相同事物正在工作。这是一个逻辑问题

标签: c# unity3d merge


【解决方案1】:

你的问题应该很清楚:你有两个对象与这个脚本发生冲突

=> 在两个组件上都调用了OnCollisionEnter2D

此外,如果您将具有相同组件的新对象放置在同一位置,则可能会立即再次为该新对象再次调用 OnCollisonEnter2D

我不认为你想要

Destroy(Triangle);

等等 => 你正试图破坏 prefabs

我认为您的代码可能应该是例如

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine;

public class Merge : MonoBehaviour 
{
    public GameObject Circle;
    public GameObject Heptagon;
    public GameObject Hexagon;
    public GameObject Octagon;
    public GameObject Pentagon;
    public GameObject Square;
    public GameObject Triangle;
    
    //NOTE: MonoBehaviour Messages like Start and Update are called even if they are empty
    //NOTE: Thus only causing unnecessary overhead => If you don't need them always remove them
    
    public void OnCollisionEnter2D(Collision2D collision)
    {
        var otherTransform = collision.transform;
        var otherObject = collision.gameObject;
    
        // In general rather use CompareTag instead of ==
        if (otherObject.CompareTag("Triangle"))
        {
            Debug.Log("Merged !!!");
            // Destroy the object we collide with
            Destroy(otherObject);
            // and our self
            Destroy(gameObject);
            // Then spawn the new object in the position of the hit object
            Instantiate(Circle, (Vector2)otherTransform.position, Quaternion.identity);
        }
        else if (otherObject.CompareTag("Circle"))
        {
            Debug.Log("Merged !!!");
            Destroy(otherObject);
            Destroy(gameObject);
            Instantiate(Square, (Vector2)otherTransform.position, Quaternion.identity);
        }
        else if (otherObject.CompareTag("Square"))
        {
            Debug.Log("Merged !!!");
            Destroy(otherObject);
            Destroy(gameObject);
            Instantiate(Pentagon, (Vector2)otherTransform.position, Quaternion.identity);
        }
        else if (otherObject.CompareTag("Pentagon"))
        {
            Debug.Log("Merged !!!");
            Destroy(otherObject);
            Destroy(gameObject);
            Instantiate(Hexagon, (Vector2)otherTransform.position, Quaternion.identity);
        }
        else if (otherObject.CompareTag("Hexagon"))
        {
            Debug.Log("Merged !!!");
            Destroy(otherObject);
            Destroy(gameObject);
            Instantiate(Heptagon, (Vector2)otherTransform.position, Quaternion.identity);
        }
        else if (otherObject.CompareTag("Heptagon"))
        {
            Debug.Log("Merged !!!");
            Destroy(otherObject);
            Destroy(gameObject);
            Instantiate(Octagon, (Vector2)otherTransform.position, Quaternion.identity);
        }
    }   
}

在销毁命中的对象和您自己实例化新对象之前,应该保存只处理一次碰撞。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2016-09-11
    相关资源
    最近更新 更多