Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity(C#)-如何在毁灭/重生系统中挑出Raycast检测到的游戏对象_C#_Unity3d_Raycasting - Fatal编程技术网

Unity(C#)-如何在毁灭/重生系统中挑出Raycast检测到的游戏对象

Unity(C#)-如何在毁灭/重生系统中挑出Raycast检测到的游戏对象,c#,unity3d,raycasting,C#,Unity3d,Raycasting,我正试图制造一个毁灭的游戏对象,等x秒,让游戏对象系统重生。我有两个脚本,我正在销毁它,然后再次实例化它。我想使用多个相同的预制件,称为“易碎”,但只有一个我的目标是被摧毁。类似于像Minecraft这样的游戏,瞄准并且只有瞄准区块的目标会被摧毁 BlockBreakItem脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; public class BlockBreakItem

我正试图制造一个毁灭的游戏对象,等x秒,让游戏对象系统重生。我有两个脚本,我正在销毁它,然后再次实例化它。我想使用多个相同的预制件,称为“易碎”,但只有一个我的目标是被摧毁。类似于像Minecraft这样的游戏,瞄准并且只有瞄准区块的目标会被摧毁

BlockBreakItem脚本:

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

public class BlockBreakItem : MonoBehaviour
{
    RaycastHit hit;
    int layerMask = 1;
    public GameObject breakableObject;
    public bool isObjectDestoryed = false;

    public int score = 0;

    // Update is called once per frame
    void Update()
    {
        breakableDetection();
    }

    void breakableDetection()
    {
        Ray rayLocation = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(rayLocation, out hit, 1000, layerMask))
        {
            print("Detected");
            if (Input.GetKey(KeyCode.Mouse0))
            {
                
                breakableObject = GameObject.Find("Breakable");
                Destroy(breakableObject);
                isObjectDestoryed = true;
                
                score = score +1 ;
            }
        }
    }
}
RespawnBrokenObject脚本:

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

public class RespawnBrokenObject : MonoBehaviour
{
    private BlockBreakItem BlockBreakItem;
    public GameObject breakablePrefab;

    // Start is called before the first frame update
    void Start()
    {
        BlockBreakItem = GameObject.FindObjectOfType<BlockBreakItem>();
    }

    // Update is called once per frame
    void Update()
    {
        if (BlockBreakItem.isObjectDestoryed == true)
        {
            Invoke("respawnObject", 5.0f);
            BlockBreakItem.isObjectDestoryed = false;
        }
    }

    void respawnObject()
    {
        GameObject tempObjName = Instantiate(breakablePrefab);
        
        tempObjName.name = breakablePrefab.name;
        BlockBreakItem.breakableObject = tempObjName;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类重生对象:单一行为
{
私有BlockBreakItem BlockBreakItem;
公共游戏对象BreakablePrefact;
//在第一帧更新之前调用Start
void Start()
{
BlockBreakItem=GameObject.FindObjectOfType();
}
//每帧调用一次更新
无效更新()
{
if(BlockBreakItem.IsObjectDestroyed==true)
{
调用(“respawnObject”,5.0f);
BlockBreakItem.IsObjectDestroyed=false;
}
}
void respawnObject()
{
GameObject tempObjName=实例化(breakablePrefab);
tempObjName.name=breakableprefable.name;
BlockBreakItem.breakableObject=tempObjName;
}
}

我希望代码不要太乱!总是担心它不会被理解xD

幸运的是,它在统一中很简单而且基本

你不能这样做:

breakableObject = GameObject.Find("Breakable");
好消息是演员会告诉你你击中了什么物体!很好,嗯

基本上是:

hit.collider.gameObject
为了方便起见,您可以在Debug.Log中使用
hit.collider.gameObject.name

就这么简单

注意,然后(如果需要的话)使用类似于
.GetComponent()
的东西在该对象上获取组件。。。简单

请注意,只需检查该组件是否存在,即可检查命中的对象是否为“YourBreakishBlock”对象之一

请注意,只需在谷歌上搜索“unity physics raycast out hit,哪个对象被击中?”之类的内容就可以了


等等

--

让这个简单的类:

public class ExamplePutThisOnACube: MonoBehavior
{
public void SAYHELLO() { Debug.Log("hello!"); }
}
现在把它放在一些立方体上,但不要放在其他立方体上

在射线代码中:

ExamplePutThisOnACube teste =
  hit.collider.gameObject.GetComponent<ExamplePutThisOnACube>();

现在运行应用程序并尝试指向各种立方体

代码看起来很整洁!要明确的是,目前的问题是什么??:O@Fattie当我在游戏中添加第二个“易碎”预制件时,我会破坏一个预制件,因为它们具有相同的名称和标签。我希望能够使用相同的名称和标签的多个相同的预制,但只有一个我直接看到被打破。我该怎么做?如果说得通的话啊!很简单!哇,这真是太简单了xD。我真的不明白.getcomponent部分是做什么的,因为我还没有学会!现在我需要弄清楚如何把积木放回原来的位置,而不是预制件的位置!我应该把它们放在哪里对不起?大脑现在正以半速运转!他们会使用哪些脚本?等等,我让它工作了!所以我可以使用某种形式的getcomponent来获取立方体的单击位置,将其传递到respawn脚本中,然后像那样设置位置吗?!天哪,还是把整个物体传递给重生者!?我让它工作了!他们现在单独工作!!!!非常感谢你!是的,你的两个陈述都是正确的!!!!顺便说一句,得到这个职位很容易<代码>命中。碰撞器。游戏对象。变换。位置!!!
if (teste != null) { teste.SAYHELLO(); }