简体   繁体   English

Unity 5. Raycast比较标记

[英]Unity 5. Raycast compare tag

I am using a raycast system to detect items in the world, and if the raycast detects an apple and I press "E" my character should pick up that specific apple. 我正在使用光线投射系统来检测世界上的物品,如果光线投射检测到一个苹果并且我按“ E”,我的角色应该捡起那个特定的苹果。 Same goes with any other objects that would be pickable. 其他任何可拾取的对象也是如此。

I can't figure out a way of doing this neatly besides hard coding every single item with an "IF" statement to check if that specific item was targeted. 除了用“ IF”语句对每个项目进行硬编码以检查该特定项目是否为目标外,我无法弄清楚如何做到这一点。

I have my thought on Comparing Tags but I don't see how I wouldn't have to hard code everything nontheless. 我对比较标签有个想法,但是我看不到如何不必对所有内容进行硬编码。

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.
}

I think you're looking for the function: GameObject.SendMessage 我认为您正在寻找的功能是: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
}

Then in your script for "enemy","Apple" or "Drink" put a function called "CheckHit" 然后在您的“敌人”,“苹果”或“饮料”脚本中放置一个名为“ CheckHit”的函数

ie

public class AppleController : MonoBehaviour
{
    public void CheckHit();
}

If you want to add variables to "CheckHit()" then call 如果要将变量添加到“ CheckHit()”,请调用

hit.collider.SendMessage("CheckHit", 1.0f);

public class AppleController : MonoBehaviour
{
    public void CheckHit(float something);
}

https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html https://docs.unity3d.com/ScriptReference/GameObject.SendMessage.html

My previous answer works if you want to call a function within the hit.transform class. 如果您想在hit.transform类中调用一个函数,则我的上一个答案有效。

I thought if you wanted to call a function from within the current Class that the following way is better. 我想,如果您想从当前Class内调用函数,则以下方法会更好。

If you wanted to call a function from the fire() function you could always do what is explained in: 如果您想从fire()函数中调用一个函数,则可以始终按照以下说明进行操作:

Calling a function from a string in C# 从C#中的字符串调用函数

just name your functions such as 只是命名您的功能,例如

public void HandleApple();
public void HandleEnemy();

get the function to call with the name using the "hit" tag 使用“ hit”标签获取带有名称的函数

There are a few ways to call a function by name described in the above link, but I think by using the "hit.transform.tag" all you would have to do is name your function related to it's tag 有几种方法可以通过上述链接中所述的名称来调用函数,但是我认为通过使用“ hit.transform.tag”,您所要做的就是命名与其标签相关的函数

if (Physics.Raycast(transform.position, transform.forward, out hit, rayRange))
string functionName = "Handle" + hit.transform.tag; // tag is "Apple"

Then invoke the function by calling the function by "functionName" ("HandleApple") 然后通过通过“ functionName”(“ HandleApple”)调用函数来调用函数

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

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