public void Move() { int stt = sun; scoreText.text = stt.ToString(); if (Input.GetKey(KeyCode.W)) { direction = 0; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[2]; this.gameObject.transform.Translate(Vector3.up * speed * Time.deltaTime); PlayFootstepSound(); } else if (Input.GetKey(KeyCode.S)) { this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[0]; direction = 2; this.gameObject.transform.Translate(Vector3.down * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.D)) { direction = 3; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[1]; this.gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime); } else if (Input.GetKey(KeyCode.A)) { direction = 1; this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[3]; this.gameObject.transform.Translate(Vector3.left * speed * Time.deltaTime); } // 攻击 if (Input.GetMouseButtonDown(0)) { // 处理鼠标左键点击事件 GameObject ins = Instantiate(bulletPrefab); Vector2 clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); ins.GetComponent<Blogs>().clickPosition = clickPosition; if (direction == 0) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y + 0.15f); } else if (direction == 2) { ins.transform.position = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y - 0.15f); } else if (direction == 3) { ins.transform.position = new Vector3(this.gameObject.transform.position.x + 0.15f, this.gameObject.transform.position.y); } else if (direction == 1) { ins.transform.position = new Vector3(this.gameObject.transform.position.x - 0.15f, this.gameObject.transform.position.y); } } }增加移动时循环播放
时间: 2023-06-23 12:10:25 浏览: 158
stt.js:语音到浏览器的文本库:microphone:
你可以尝试在Move()函数中加入一个循环,在循环内检测输入的方向键并移动角色,直到玩家松开方向键为止。同时你可以在移动时循环播放角色行走的音效,代码如下:
public AudioClip footstepSound; // 定义角色行走音效
public void Move()
{
int stt = sun;
scoreText.text = stt.ToString();
// 播放角色行走音效
PlayFootstepSound();
while (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
if (Input.GetKey(KeyCode.W)) {
direction = 0;
this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[2];
this.gameObject.transform.Translate(Vector3.up * speed * Time.deltaTime);
} else if (Input.GetKey(KeyCode.S)) {
this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[0];
direction = 2;
this.gameObject.transform.Translate(Vector3.down * speed * Time.deltaTime);
} else if (Input.GetKey(KeyCode.D)) {
direction = 3;
this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[1];
this.gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime);
} else if (Input.GetKey(KeyCode.A)) {
direction = 1;
this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[3];
this.gameObject.transform.Translate(Vector3.left * speed * Time.deltaTime);
}
}
}
void PlayFootstepSound()
{
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
AudioSource.PlayClipAtPoint(footstepSound, transform.position);
}
}
在上面的代码中,我们添加了一个while循环,在循环中检测玩家输入的方向键并移动角色,直到玩家松开方向键为止。同时我们在移动时循环播放角色行走音效,通过调用PlayFootstepSound()函数实现。
阅读全文