【发布时间】:2020-01-16 20:19:56
【问题描述】:
每当对象旋转通过某个点时,我都会尝试播放声音。代码工作正常,但突然停止了,我不知道还能做什么。
对象是一扇门,根据 Unity 的变换信息,它沿其 Z 轴从 -180 旋转到 -300。我希望在门 transform.rotation.z 小于 -190 时播放声音“portaFechando”,但它不起作用。
我只能听到“portaAbrindo”的声音。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class abrirPorta : MonoBehaviour
{
Animator anim;
bool portaFechada = true;
public AudioSource audio;
public AudioClip abrindo;
public AudioClip fechando;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
// checkando input para abrir a porta
if (Input.GetKeyDown("space") && portaFechada == true)
{
anim.SetBool("portaFechada", false);
anim.SetFloat("portaSpeed", 1);
portaFechada = false;
audio.clip = abrindo;
audio.Play();
}
// checkando input para fechar porta
else if (Input.GetKeyDown("space") && portaFechada == false)
{
anim.SetBool("portaFechada", true);
anim.SetFloat("portaSpeed", -1);
portaFechada = true;
}
// tocando som de fechando checkando rotação (bugou)
if (portafechada == false && transform.rotation.z <= -190)
{
Debug.Log("Worked!");
audio.clip = fechando;
audio.Play();
}
}
}
【问题讨论】: