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); } 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-24 21:04:30 浏览: 204
stt.js:语音到浏览器的文本库:microphone:
要实现移动时脚步声,可以在Move()方法中添加相关代码。首先,需要在脚本中定义一个AudioSource类型的变量,用于播放脚步声音效。然后,在移动的过程中,每隔一段时间(例如0.5秒)播放一次脚步声。具体的实现代码如下:
public class PlayerController : MonoBehaviour
{
public float speed = 3.0f;
public GameObject bulletPrefab;
public Sprite[] prota;
public Text scoreText;
public AudioClip footstepSound;
private int direction = 0; // 0-up, 1-left, 2-down, 3-right
private int sun = 0;
private AudioSource audioSource;
private float footstepInterval = 0.5f;
private float footstepTimer = 0.0f;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void Update()
{
Move();
}
public void Move()
{
int stt = sun;
scoreText.text = stt.ToString();
footstepTimer += Time.deltaTime;
if (Input.GetKey(KeyCode.W))
{
direction = 0;
this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[2];
this.gameObject.transform.Translate(Vector3.up * speed * Time.deltaTime);
if (footstepTimer > footstepInterval)
{
audioSource.PlayOneShot(footstepSound);
footstepTimer = 0.0f;
}
}
else if (Input.GetKey(KeyCode.S))
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[0];
direction = 2;
this.gameObject.transform.Translate(Vector3.down * speed * Time.deltaTime);
if (footstepTimer > footstepInterval)
{
audioSource.PlayOneShot(footstepSound);
footstepTimer = 0.0f;
}
}
else if (Input.GetKey(KeyCode.D))
{
direction = 3;
this.gameObject.GetComponent<SpriteRenderer>().sprite = prota[1];
this.gameObject.transform.Translate(Vector3.right * speed * Time.deltaTime);
if (footstepTimer > footstepInterval)
{
audioSource.PlayOneShot(footstepSound);
footstepTimer = 0.0f;
}
}
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 (footstepTimer > footstepInterval)
{
audioSource.PlayOneShot(footstepSound);
footstepTimer = 0.0f;
}
}
// 攻击
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);
}
}
}
}
阅读全文