2017-03-07 1 views
1

オブジェクトを見てボタンを押すと、何かする必要があります。私が初めてそれをするとき、それは動作しますが、ボタンをもう一度押す必要はありません。ただオブジェクトを見ることができます。しかし、選手だけではなく、私はそれを使用する方法RaycastHitは常に真を返します

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プレイヤーの衝突を無視します。ゲーム

enter image description here

+2

あなた自身を打っているので、それはtrueを返します。 'Ray_ray = Camera.main.ScreenPointToRay(新しいVector3(Screen.width/2、Screen.height/2、0));' 'Z'をさらに遠くに設定します。 「2」。それ以外は常にそれ自体を打つことを期待しています... –

+0

@ m.rogalskiここでは、プレーヤーのコライダーを無視します: 'int layerMask = 1 << 9; layerMask =〜layerMask; '。私はその物体にぶつけることができる。しかし、私が2度目にそれをヒットしたいとき、私はボタンを押す必要はありません、私はちょうどオブジェクトを見ることができます – dima

答えて

1

わかりましたので、基本的にしている設定しますActionNumber1(さんが言わせて)に、それは、この値にとどまるからVideo。あなたはその値の時間ベースのリセットを設定するか、または単にUpdate(またはLateUpdate)にRaycastのすべての時間を使用する必要がありますこれを解決するには


もう1つの方法はevent driven programming principlesを使用し、条件が満たされたときにイベントを発生させ、値の設定を忘れることです。十分にそれは簡単な作り

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

とコードでこれを使用する:

public OnMousePressCasino onMousePressCasinoBarthender; 
public OnMousePressCasino onMousePressCasinoDoorToStreet; 

void Start() 
{ 
    onMousePressCasinoBarthender.OnAction += MeAction; 
    onMousePressCasinoDoorToStreet.OnAction += MeAction; 
} 

void MeAction(object sender, MeEventArgs e) 
{ 
    if(e.Action == 1) 
    { 
     // do something 
    } 
    else if (e.Action == 2) 
    { 
     // do something else. 
    } 
} 
+1

"これは基本的にあなたが設定しているActionNumber(言いましょう)1とそれはこの値にとどまりますあなたが別のオブジェクトをクリックするまで "。ではない正確に。私はこのオブジェクトをそれぞれのオブジェクトに貼り付けました。だから私はオブジェクトAを押してオブジェクトBを押すことができますし、オブジェクトAを押す必要はありません。ちょうどそれを見てみることができます。しかし、ありがとう、私はそれを試してみます – dima

+0

@dimaはい。それが私の言いたいことです。私の間違いを指摘してくれてありがとう:) –

関連する問題