2016-08-08 5 views
2

私は2つのコライダーを備えた食器棚を持っています.1つは食器棚用、もう1つはボックス用です。私がボックスを押すと、私はそれを開閉したいと思う。それはうまくいきましたが、何らかの理由で、それはボックスの端を押すと機能します。センターをクリックすると機能しません。ボックスコライダーはマウスボタンプレスイベントを受け取りません

ビデオ:

public Animation[] animations; 
public string[] animationName; 
public bool playOneDirection; // should revert animation speed after second playing? 
public AudioSource myAudioOpen; 
public AudioSource myAudioClose; 

private bool isDoorClosed; 
private bool isAimationReadyToPlay = true; 
private Collider thisCollider; 

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

void Update() 
{ 
    if (Input.GetButton("Fire1")) 
     if(DoPlayerLookAtButton() && isAimationReadyToPlay) 
      OpenCloseDoor(); 
} 

bool DoPlayerLookAtButton() 
{ 
    RaycastHit _hit; 
    Ray _ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width/2, Screen.height/2, 0)); 
    bool isHit = Physics.Raycast(_ray, out _hit, 1.5f); 

    if (isHit && _hit.collider == thisCollider) return true; 
    else return false; 
} 

public void OpenCloseDoor() 
{ 
    if (!isDoorClosed) // Play animation with normal speed 
    { 
     myAudioOpen.Play(); 
     for (int i = 0; i < animations.Length; i++) 
     { 
      animations[i][animationName[i]].speed = 1.0f; 
      animations[i].Play(); 
     } 
    } 

    if(playOneDirection) 
     return; 

    if (isDoorClosed) // Play animation with revert speed 
    { 
     myAudioClose.Play(); 
     for (int i = 0; i < animations.Length; i++) 
     { 
      animations[i][animationName[i]].speed = -1.0f; 
      animations[i][animationName[i]].time = animations[i][animationName[i]].length; 
      animations[i].Play(); 
     } 
    } 

    StartCoroutine("DelayBetweenAnimations"); 
    isDoorClosed = !isDoorClosed; 
} 

IEnumerator DelayBetweenAnimations() 
{ 
    isAimationReadyToPlay = false; 
    yield return new WaitForSeconds(0.5f); 
    isAimationReadyToPlay = true; 
} 
+1

に達しなかった

。私にはいくつかのアイディアがあります。 1.他のいくつかのコライダーが途中にあります。 2.カメラクリッププレーンがコライダーをカットしますか?最後の1つは、私はそれが可能であると確信していません。 –

答えて

1

あなたの食器棚が2つのコライダーを持っていますが、あなただけです:https://youtu.be/OozsAi7KNzs

ここで私は箱の上に押したときに、アニメーション(オープン/クローズ食器棚)を再生コードは、ありますそのうちの1つをチェックする。重複がある場合は、正しいものをクリックするのが面倒かもしれません。あなただけの...どこでもゲームオブジェクトをクリックので、同じようにコードを変更することができ

//From 
//if (isHit && _hit.collider == thisCollider) return true; 
//To 
if (isHit && _hit.transform.gameObject == this.gameObject) return true; 

プレーヤーのためのレイヤーマスクを追加し、Physics.Raycastを確保したい場合は、キャストを回避するために、その層を除外あなた自身を打つことから。 See here

0

私は主カメラをプレーヤーの中心から始めるので、レイキャストはプレーヤーのコライダーに当たっています。私はそれがカメラが下の画面のようにthrouth壁に行くことができるときにバグを修正しようとしました。レイキャストがプレイヤーを通過見ることができ、あなたの問題を引き起こしているものを言うのは本当に難しいですボックス enter image description here

+1

私の答えを見てください。 –

関連する問題