0
\$\begingroup\$

I have a cupboard with 2 colliders - one for cupboard and one for it's box. When I press on the box, I want to open/close it. It worked fine, but now by some reason it only work when I press on the edge on the box. When click on the center, it don't work.

Video: https://youtu.be/OozsAi7KNzs

Here is the code, which play animation (open/close cupboard), when I press on the box:

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; } 
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

You are more than likely hitting another collider before reaching the cabinet collider.

Try using RaycastAll instead. Something like this:

RaycastHit[] hits = Physics.RaycastAll(_ray, out _hit, 1.5f); for (int i = 0; i < hits.Length; i++) { RaycastHit hit = hits[i]; //check if cabinet if (hit.gameObject.tag == "Cabinet") { if (isHit && _hit.collider == thisCollider) return true; else return false; } } 
\$\endgroup\$
0
\$\begingroup\$

I've made the main camera starting from the center of the player, so raycast hits player's collider. I made it trying to fix the bug when camera can go throuth the wall like on the screen below.

Raycast can be seen passing through the player and did not reach the box

enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ The debug raycast won't stop when it hits something, whereas Physics.Raycast will. I'd try @Jon 's suggestion to use RaycastAll instead, and see if that helps \$\endgroup\$ Commented Aug 9, 2016 at 14:55

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.