2017-03-07 3 views
1

일부 오브젝트를보고 버튼을 누르면 뭔가해야합니다. 처음으로 해보면 작동하지만 버튼을 다시 누를 필요가 없습니다. 객체를 볼 수 있습니다. 그러나 선수뿐만 아니라 내가 그것을 사용하는 방법RaycastHit가 항상 true를 반환합니다.

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 

편집 한 플레이어의 입자 가속기를 무시.

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; '. 나는 그 물건을 칠 수있다. 하지만 두 번째로 치고 싶을 때, 버튼을 누르지 않아도됩니다. 객체를 볼 수 있습니다. – dima

답변

1

좋아 게임에서 Video 그래서 기본적으로이 설정은 ActionNumber에있어 (의 말)하자 1과이 값에서 유지됩니다.

이 문제를 해결하려면 해당 값의 시간 기반 재설정을 설정하거나 Update (또는)으로 항상 Raycast을 사용해야합니다.
또 다른 방법은 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를 누르지 않아도됩니다. 그런데 B처럼 보일 수 있습니다.하지만 고마워요. 시도해 볼게요. – dima

+0

@dima 예. 그것이 내가 의미했던 것입니다. 내 실수를 가리켜 주셔서 감사합니다 :) –

관련 문제