Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

unity3d - 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. I have 2 scripts and I'm destorying then instantiating it again. I want to use multiples of the same prefab called "Breakable" but have only the one I'm aiming at being destroyed. Similar to games like Minecraft, aim and only the aimed at the block is destroyed.

BlockBreakItem script:

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:

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

question from:https://stackoverflow.com/questions/65855280/unity-c-how-do-i-single-out-gameobject-being-detected-by-raycast-in-a-destr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Fortunately it's easy and basic in Unity!

You don't do this:

breakableObject = GameObject.Find("Breakable");

The good news is the cast will tell you what object you hit! Great, eh?

It's basically:

hit.collider.gameObject

You can use hit.collider.gameObject.name in a Debug.Log for convenience.

It's that easy!

Note you then (if you need it) get your component on that object with something like .GetComponent<YourBreakishBlock>()... easy.

Note that you can CHECK if the object hit, is one of the "YourBreakishBlock" objects, by simply checking if that component is present.

Note simply google something like "unity physics raycast out hit, which object was hit ?" for endless examples,

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

etc.

--

Make this simple 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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.5k users

...