RaycastHit一直返回true

时间:2017-03-07 10:42:40

标签: c# unity3d

当我看一些物体并按下按钮时,我需要做点什么。当我第一次这样做时,它可以工作,但是我不需要再次按下按钮,我只能看看物体。但玩家必须看对象并按下按钮,不仅要看

private Collider thisCollider;
public int ActionNumber { get; private set; }

void Start ()
{
    thisCollider = GetComponent<Collider>();
}

void Update ()
{
    if (Input.GetButton("Fire1") && DoPlayerLookAtObject())
        ActionsList();
}

bool DoPlayerLookAtObject()
{
    int layerMask = 1 << 9;
    layerMask = ~layerMask;

    RaycastHit _hit;
    Ray _ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
    bool isHit = Physics.Raycast(_ray, out _hit, 2.0f, layerMask);        
    if (isHit && _hit.collider == thisCollider)
        return true; // this return true all the time after first interaction with object
    else
        return false;
}

public bool ActionsList()
{
    if (DoPlayerLookAtObject())
        switch (thisCollider.name)
        {
            case "barthender":   ActionNumber = 1; return true;
            case "doorToStreet": ActionNumber = 2; return true;
            default: Debug.Log("Error: Out of range"); break;
        }
    return false;
}

我如何使用它:

public OnMousePressCasino onMousePressCasinoBarthender;
public OnMousePressCasino onMousePressCasinoDoorToStreet;

if (onMousePressCasinoBarthender.ActionNumber == 1 &&
    onMousePressCasinoBarthender.ActionsList())
    // do something

if (onMousePressCasinoDoorToStreet.ActionNumber == 2 &&
    onMousePressCasinoDoorToStreet.ActionsList())
    // do something

编辑1 忽略玩家的对手。来自游戏的Video

enter image description here

1 个答案:

答案 0 :(得分:1)

好的,所以基本上你将ActionNumber设置为(让我们说)1并保持此值。

要解决此问题,您必须设置基于时间的重置值,或者只在[{1}}(或Raycast)中使用Update
另一种方法是使用event driven programming principles并在遇到条件时触发事件并忘记设置某些值。

让它变得简单:

LateUpdate

现在创建private Collider thisCollider; public event EventHandler<MeEventArgs> OnAction; void Start() { thisCollider = GetComponent<Collider>(); } void Update () { if (Input.GetButtonDown("Fire1")) { EventHandler<MeEventArgs> handler = OnAction; int actionIndex = DoPlayerLookAtObject(); if ( handler != null && actionIndex >= 0) { handler(this, new MeEventArgs(actionIndex)); } } } int DoPlayerLookAtObject() { int layerMask = 1 << 9; layerMask = ~layerMask; RaycastHit _hit; Ray _ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)); bool isHit = Physics.Raycast(_ray, out _hit, 2.0f, layerMask); //if (isHit && _hit.collider == thisCollider) // return true; // this return true all the time after first interaction with object //else // return false; if (isHit && _hit.collider == thisCollider) return ActionList(); return -1; } public int ActionsList() { int result = -1; switch (thisCollider.name) { case "barthender": result = 1; break; case "doorToStreet": result = 2; break; default: Debug.Log("Error: Out of range"); break; } return result; }

MeEventArgs

并在代码中使用它:

public class MeEventArgs : EventArgs
{
    public readonly int Action;

    public MeEventArgs(int actionIndex) : base()
    {
        Action = actionIndex;
    }
}