일부 오브젝트를보고 버튼을 누르면 뭔가해야합니다. 처음으로 해보면 작동하지만 버튼을 다시 누를 필요가 없습니다. 객체를 볼 수 있습니다. 그러나 선수뿐만 아니라 내가 그것을 사용하는 방법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
편집 한 플레이어의 입자 가속기를 무시.
그것은 true를 돌려줍니다. 'Ray _ray = Camera.main.ScreenPointToRay (새 Vector3 (Screen.width/2, Screen.height/2, 0));''Z'를 멀리 설정합니다. '2'. 게다가 그것은 항상 자신을 치기를 기대합니다. –
@ m.rogalski 여기에 플레이어의 충돌자를 무시합니다 :'int layerMask = 1 << 9; layerMask = ~ layerMask; '. 나는 그 물건을 칠 수있다. 하지만 두 번째로 치고 싶을 때, 버튼을 누르지 않아도됩니다. 객체를 볼 수 있습니다. – dima