전체 무브먼트 코드입니다.
public class MoveControll : MonoBehaviour {
public float Mspeed = 0.0f;
public float Jspeed = 0.0f;
public float MouseSense = 0.0f;
public float updownRange = 90;
public AudioClip flashsound;
private float Rotleftright;
private float Rotvertical;
private bool isjump = false;
private Rigidbody rigid;
// Use this for initialization
void Start () {
rigid = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("flashlight"))
{
flash();
}
}
void FixedUpdate()
{
Move();
Jump();
Rotate();
}
void Move()
{
if (Input.GetAxisRaw("Horizontal") > 0)
{
transform.Translate(Vector3.right * Mspeed * Time.deltaTime);
}
else if (Input.GetAxisRaw("Horizontal") < 0)
{
transform.Translate(Vector3.left * Mspeed * Time.deltaTime);
}
else if (Input.GetAxisRaw("Horizontal") == 0)
{
}
if (Input.GetAxisRaw("Vertical") > 0)
{
transform.Translate(Vector3.forward * Mspeed * Time.deltaTime);
StartCoroutine("footstep");
}
else if (Input.GetAxisRaw("Vertical") < 0)
{
transform.Translate(Vector3.back * Mspeed * Time.deltaTime);
StartCoroutine("footstep");
}
else if (Input.GetAxisRaw("Vertical") == 0)
{
}
}
void Jump()
{
if (Input.GetButtonDown("Jump"))
{
if (isjump == true)
return;
else if (isjump == false)
{
rigid.AddForce(Vector3.up * Jspeed, ForceMode.Impulse);
isjump = true;
}
}
}
void Rotate()
{
//좌우 회전
Rotleftright = Input.GetAxis("Mouse X") * MouseSense;
transform.Rotate(0f, Rotleftright, 0f);
//상하 회전
Rotvertical -= Input.GetAxis("Mouse Y") * MouseSense;
Rotvertical = Mathf.Clamp(Rotvertical, -updownRange, updownRange);
Camera.main.transform.localRotation = Quaternion.Euler(Rotvertical, 0f, 0f);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Ground")
{
isjump = false;
}
}
void flash()
{
if(GameObject.Find("Spotlight").GetComponent<Light>().enabled == false)
{
GameObject.Find("Spotlight").GetComponent<Light>().enabled = true;
AudioSource.PlayClipAtPoint(flashsound,transform.position);
}
else
{
GameObject.Find("Spotlight").GetComponent<Light>().enabled = false;
AudioSource.PlayClipAtPoint(flashsound, transform.position);
}
}
IEnumerator footstep()
{
GetComponent<AudioSource>().Play();
yield return new WaitForSeconds(0.3f);
GetComponent<AudioSource>().Play();
}
}
예상은 했지만 정말 이런식으로 짜놨네. 코루틴이 매 프레임마다 생성되서 매 프레임마다 발소리가 Play 되니까. 버튼을 떼고 0.3초후에 마지막 소리만 잠깐 들리겠지.
누르고 있는 동안 시간체크해서 발소리를 플레이 하는 방향으로 해라. 보통 이런식으로 한다. 뭐. 근데 이 것도 문제가 몇가지 있긴하지.
Update 가 매 프레임마다 자동으로 1번씩 실행된다는 거 잊지 말고 코루틴이 알아서 Update를 지연시켜준다고 착각하지만 않으면 된다.
클래스 이름을 확인해보자. 트롤이다.. 그래서 안된 것 아닐까? 합리적 의심;
죄송한데.. 누르고 있는동안 시간체크해서 플레이 하라는거 이해를 잘 못하겠는데 어떻게 하라는거에요??
나같으면 bool하나 만들어서 키다운때 bool true일때만 코루틴 생성, bool변수 false 하고 코루틴에서 키업때까지 냅두다가 키업들어오면 플레이 멈추고 코루틴 멈출거같은데
ㄴ 착하네
불 하나 넣으라는게 코루틴에서 오디오 재생 후 트루로 바꾸고 시간 지난 후 펄스로 되돌린후 펄스일때만 재생되게 하라 이 소린가요? - dc App