Unity (C#) - 如何在销毁/重生系统中挑出 Raycast 检测到的 GameObject
Unity (C#) - How do I single out GameObject being detected by Raycast in a destroy / respawn system
我正在尝试创建一个销毁游戏对象,等待 x 秒,重生游戏对象系统。我有 2 个脚本,我正在破坏然后再次实例化它。我想使用多个名为“Breakable”的相同预制件,但只有一个我打算销毁的预制件。类似于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;
}
}
希望代码不要太乱!总担心不被人理解xD
幸运的是,在 Unity 中这很简单而且很基础!
你不这样做:
breakableObject = GameObject.Find("Breakable");
好消息是演员会告诉你你击中了什么物体!太好了,嗯?
基本上是:
hit.collider.gameObject
为了方便起见,您可以在 Debug.Log 中使用 hit.collider.gameObject.name
。
就这么简单!
注意你然后(如果你需要的话)用类似.GetComponent<YourBreakishBlock>()
的东西在那个对象上得到你的组件......简单。
请注意,您可以通过简单地检查该组件是否存在来检查命中的对象是否是“YourBreakishBlock”对象之一。
只需注意 google 类似“unity physics raycast out hit, which object was hit?”之类的东西。对于无穷无尽的例子,
https://forum.unity.com/threads/getting-object-hit-with-raycast.573982/
https://forum.unity.com/threads/changing-properties-of-object-hit-with-raycast.538819/
等等
--
让这个变得简单 class:
public class ExamplePutThisOnACube: MonoBehavior
{
public void SAYHELLO() { Debug.Log("hello!"); }
}
现在把它放在一些立方体上,但不要放在其他立方体上。
在你的射线代码中:
ExamplePutThisOnACube teste =
hit.collider.gameObject.GetComponent<ExamplePutThisOnACube>();
然后检查一下:
if (teste != null) { teste.SAYHELLO(); }
现在 运行 应用程序并尝试指向各种立方体!
我正在尝试创建一个销毁游戏对象,等待 x 秒,重生游戏对象系统。我有 2 个脚本,我正在破坏然后再次实例化它。我想使用多个名为“Breakable”的相同预制件,但只有一个我打算销毁的预制件。类似于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;
}
}
希望代码不要太乱!总担心不被人理解xD
幸运的是,在 Unity 中这很简单而且很基础!
你不这样做:
breakableObject = GameObject.Find("Breakable");
好消息是演员会告诉你你击中了什么物体!太好了,嗯?
基本上是:
hit.collider.gameObject
为了方便起见,您可以在 Debug.Log 中使用 hit.collider.gameObject.name
。
就这么简单!
注意你然后(如果你需要的话)用类似.GetComponent<YourBreakishBlock>()
的东西在那个对象上得到你的组件......简单。
请注意,您可以通过简单地检查该组件是否存在来检查命中的对象是否是“YourBreakishBlock”对象之一。
只需注意 google 类似“unity physics raycast out hit, which object was hit?”之类的东西。对于无穷无尽的例子,
https://forum.unity.com/threads/getting-object-hit-with-raycast.573982/
https://forum.unity.com/threads/changing-properties-of-object-hit-with-raycast.538819/
等等
--
让这个变得简单 class:
public class ExamplePutThisOnACube: MonoBehavior
{
public void SAYHELLO() { Debug.Log("hello!"); }
}
现在把它放在一些立方体上,但不要放在其他立方体上。
在你的射线代码中:
ExamplePutThisOnACube teste =
hit.collider.gameObject.GetComponent<ExamplePutThisOnACube>();
然后检查一下:
if (teste != null) { teste.SAYHELLO(); }
现在 运行 应用程序并尝试指向各种立方体!