Ich habe einen Schrank mit 2 Colliders - eine für Schrank und eine für seine Box. Wenn ich auf die Box drücke, möchte ich sie öffnen/schließen. Es hat gut funktioniert, aber aus irgendeinem Grund funktioniert es nur, wenn ich auf den Rand der Box drücke. Wenn Sie auf die Mitte klicken, funktioniert es nicht.Box Collider fängt keine Maus drücken drücken Ereignis
Video: https://youtu.be/OozsAi7KNzs
Hier ist der Code, die Animation spielt (Öffnen/Schließen Schrank), wenn ich auf dem Feld drücken:
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;
}
Es ist wirklich schwer zu sagen, was das Problem verursacht. Ich habe wenige Ideen. 1. Ein anderer Collider ist unterwegs. 2. Kamera-schneidende Flugzeuge schneiden den Collider? Der letzte bin ich mir nicht sicher, dass es möglich ist. –