简体   繁体   English

Unity (C#) - 如何在销毁/重生系统中挑选出被 Raycast 检测到的 GameObject

[英]Unity (C#) - How do I single out GameObject being detected by Raycast in a destroy / respawn system

I'm trying to make a Destroy gameobject, wait x seconds, respawn gameobject system.我正在尝试制作一个 Destroy 游戏对象,等待 x 秒,重新生成游戏对象系统。 I have 2 scripts and I'm destorying then instantiating it again.我有 2 个脚本,我正在破坏然后再次实例化它。 I want to use multiples of the same prefab called "Breakable" but have only the one I'm aiming at being destroyed.我想使用多个名为“Breakable”的相同预制件,但只有一个我打算被摧毁的预制件。 Similar to games like Minecraft, aim and only the aimed at the block is destroyed.类似于 Minecraft 之类的游戏,瞄准并且只有瞄准的方块会被摧毁。

BlockBreakItem script: 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 script: 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;
    }
}

I hope the code isn't too messy!我希望代码不要太乱! Always worried it won't be understood xD总是担心它不会被理解xD

Fortunately it's easy and basic in Unity!幸运的是,它在 Unity 中既简单又基本!

You don't do this:你不这样做:

breakableObject = GameObject.Find("Breakable");

The good news is the cast will tell you what object you hit , Great?好消息是演员会告诉你你击中了什么 object ,好吗? eh?嗯?

It's basically:基本上是:

hit.collider.gameObject

You can use hit.collider.gameObject.name in a Debug.Log for convenience.为方便起见,您可以在 Debug.Log 中使用hit.collider.gameObject.name

It's that easy!就这么简单!

Note you then (if you need it) get your component on that object with something like .GetComponent<YourBreakishBlock>() ... easy.然后请注意(如果需要)使用.GetComponent<YourBreakishBlock>()类的东西在 object 上获取您的组件 ... 简单。

Note that you can CHECK if the object hit, is one of the "YourBreakishBlock" objects, by simply checking if that component is present.请注意,您可以检查 object 是否是“YourBreakishBlock”对象之一,只需检查该组件是否存在即可。

Note simply google something like "unity physics raycast out hit, which object was hit?"请注意谷歌类似“统一物理射线投射命中,哪个 object 被命中?” for endless examples,举不胜举的例子,

https://forum.unity.com/threads/getting-object-hit-with-raycast.573982/ https://forum.unity.com/threads/getting-object-hit-with-raycast.573982/
https://forum.unity.com/threads/changing-properties-of-object-hit-with-raycast.538819/ https://forum.unity.com/threads/changeing-properties-of-object-hit-with-raycast.538819/

etc.等等

-- --

Make this simple class:制作这个简单的 class:

public class ExamplePutThisOnACube: MonoBehavior
{
public void SAYHELLO() { Debug.Log("hello!"); }
}

Now put that on SOME cubes but NOT on OTHER cubes.现在把它放在一些立方体上,但不要放在其他立方体上。

In your ray code:在您的光线代码中:

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

and then check this out:然后检查一下:

if (teste != null) { teste.SAYHELLO(); }

Now run the app and try pointing at the various cubes!现在运行应用程序并尝试指向各种立方体!

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

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