Unity 5. Raycast 比较标签
Unity 5. Raycast compare tag
我正在使用光线投射系统来检测世界中的物品,如果光线投射检测到一个苹果并且我按下 "E",我的角色应该拿起那个特定的苹果。其他任何可拾取的对象也是如此。
除了用 "IF" 语句对每个项目进行硬编码以检查该特定项目是否被定位之外,我想不出一种巧妙的方法。
我对比较标签有自己的想法,但我不明白如何才能不必对所有内容进行硬编码。
void Fire()
{
if (Physics.Raycast(transform.position, transform.forward, out hit, rayRange))
{
// If (Raycast hit enemy) Damage Enemy Accordingly based on weapon damage.
// If (Raycast hit Apple) Pick up apple.
// If (Raycast hit Drink) Pick up drink.
Destroy(hit.transform.gameObject); // Debug Only
Debug.Log(hit.transform.name); // Debug Only
}
nextFire = Time.time + fireRate; // Don't mind this.
}
我认为您正在寻找函数:GameObject.SendMessage
if (Physics.Raycast(transform.position, transform.forward, out hit, rayRange))
{
hit.collider.SendMessage("CheckHit");
// Destroy(hit.transform.gameObject); // Debug Only
// Debug.Log(hit.transform.name); // Debug Only
}
然后在 "enemy"、"Apple" 或 "Drink" 的脚本中放置一个名为 "CheckHit"
的函数
即
public class AppleController : MonoBehaviour
{
public void CheckHit();
}
如果要向 "CheckHit()" 添加变量,则调用
hit.collider.SendMessage("CheckHit", 1.0f);
public class AppleController : MonoBehaviour
{
public void CheckHit(float something);
}
https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html
如果您想在 hit.transform class.
中调用函数,我之前的回答是有效的
我想如果你想从当前 Class 中调用一个函数,下面的方法更好。
如果您想从 fire() 函数中调用一个函数,您总是可以按照以下说明进行操作:
Calling a function from a string in C#
只需将您的函数命名为
public void HandleApple();
public void HandleEnemy();
使用 "hit" 标签获取要调用的函数
上面 link 中描述了几种按名称调用函数的方法,但我认为通过使用 "hit.transform.tag",您所要做的就是命名与其相关的函数标签
if (Physics.Raycast(transform.position, transform.forward, out hit, rayRange))
string functionName = "Handle" + hit.transform.tag; // tag is "Apple"
然后通过"functionName"("HandleApple")
调用函数调用函数
我正在使用光线投射系统来检测世界中的物品,如果光线投射检测到一个苹果并且我按下 "E",我的角色应该拿起那个特定的苹果。其他任何可拾取的对象也是如此。
除了用 "IF" 语句对每个项目进行硬编码以检查该特定项目是否被定位之外,我想不出一种巧妙的方法。
我对比较标签有自己的想法,但我不明白如何才能不必对所有内容进行硬编码。
void Fire()
{
if (Physics.Raycast(transform.position, transform.forward, out hit, rayRange))
{
// If (Raycast hit enemy) Damage Enemy Accordingly based on weapon damage.
// If (Raycast hit Apple) Pick up apple.
// If (Raycast hit Drink) Pick up drink.
Destroy(hit.transform.gameObject); // Debug Only
Debug.Log(hit.transform.name); // Debug Only
}
nextFire = Time.time + fireRate; // Don't mind this.
}
我认为您正在寻找函数:GameObject.SendMessage
if (Physics.Raycast(transform.position, transform.forward, out hit, rayRange))
{
hit.collider.SendMessage("CheckHit");
// Destroy(hit.transform.gameObject); // Debug Only
// Debug.Log(hit.transform.name); // Debug Only
}
然后在 "enemy"、"Apple" 或 "Drink" 的脚本中放置一个名为 "CheckHit"
的函数即
public class AppleController : MonoBehaviour
{
public void CheckHit();
}
如果要向 "CheckHit()" 添加变量,则调用
hit.collider.SendMessage("CheckHit", 1.0f);
public class AppleController : MonoBehaviour
{
public void CheckHit(float something);
}
https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html
如果您想在 hit.transform class.
中调用函数,我之前的回答是有效的我想如果你想从当前 Class 中调用一个函数,下面的方法更好。
如果您想从 fire() 函数中调用一个函数,您总是可以按照以下说明进行操作:
Calling a function from a string in C#
只需将您的函数命名为
public void HandleApple();
public void HandleEnemy();
使用 "hit" 标签获取要调用的函数
上面 link 中描述了几种按名称调用函数的方法,但我认为通过使用 "hit.transform.tag",您所要做的就是命名与其相关的函数标签
if (Physics.Raycast(transform.position, transform.forward, out hit, rayRange))
string functionName = "Handle" + hit.transform.tag; // tag is "Apple"
然后通过"functionName"("HandleApple")
调用函数调用函数